Asp.net 如何从Active directory MVC 4获取用户详细信息?
我正在使用ASP.NET MVC4(C#)开发一个web应用程序,我在其中使用LDAP(Active Directory)创建了登录系统,但我不知道如何在登录后检索登录信息,如全名、电子邮件等 登录信息将显示在每页的页眉上 以下是我的登录代码:Asp.net 如何从Active directory MVC 4获取用户详细信息?,asp.net,asp.net-mvc,asp.net-mvc-4,ldap,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Ldap,我正在使用ASP.NET MVC4(C#)开发一个web应用程序,我在其中使用LDAP(Active Directory)创建了登录系统,但我不知道如何在登录后检索登录信息,如全名、电子邮件等 登录信息将显示在每页的页眉上 以下是我的登录代码: [HttpPost] public ActionResult Login(LoginModel model, string returnUrl) { if (!this.ModelState.IsValid)
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (!this.ModelState.IsValid)
{
return View(model);
}
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "home");
}
this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
return this.View(model);
}
我在控制器中找到了答案,代码如下:
var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, id);
var EmployeeID= principal.SamAccountName;
var firstName = principal.GivenName;
var lastname = principal.Surname;
我设法获得了详细的登录信息:)