【发布时间】:2020-07-22 08:59:19
【问题描述】:
我试图通过 Windows 身份验证访问我的 ASP.NET Core MVC 项目中用户的名字、姓氏等用户信息。我实际上是在网上搜索解决方案后让它工作的,但我对这些东西和初学者级程序员很陌生,所以不明白我只是在我的项目中复制粘贴的部分发生了什么。
我在那个网站上也找不到任何解释。如果有人可以向我解释这一点,我会非常高兴。非常感谢。
此代码的网站参考:https://sensibledev.com/how-to-get-user-details-from-active-directory/
家庭控制器:
var username = User.Identity.Name;
using (var context = new PrincipalContext(ContextType.Domain, "yourdomain"))
{
var user = UserPrincipal.FindByIdentity(context, username);
if (user != null)
{
ViewData["UserName"] = user.Name;
ViewData["EmailAddress"] = user.EmailAddress;
ViewData["FullName"] = user.DisplayName;
ViewData["GivenName"] = user.GivenName;
}
}
【问题讨论】:
标签: active-directory asp.net-core-mvc windows-authentication principalcontext