如何从 Active Directory MVC 4 获取用户详细信息?

How to get userdetail from Active directory MVC 4?

我正在使用 ASP.NET MVC4 (C#) 开发 Web 应用程序,我设法使用 LDAP (Active Directory) 创建登录系统,但我不知道如何检索登录信息,例如登录后作为全名、电子邮件等。

因为登录信息将显示在每个页面的 header 上。

这是我的登录代码:

[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;

我成功获取了详细的登录信息:)