【问题标题】:Retrieve user details from Active Directory from within an ASP MVC 5 application从 ASP MVC 5 应用程序中的 Active Directory 检索用户详细信息
【发布时间】:2016-10-21 14:05:23
【问题描述】:

我有一个使用 Windows 身份验证设置的 MVC 5 Intranet 应用程序。我是 Active Directory 的新手。如何从我的 MVC 应用程序中查询 Active Directory 以检索当前经过身份验证的用户详细信息,例如电子邮件地址、电话等 - 所以基本上通过 Windows 身份验证,我所拥有的只是用户名?

一些代码会有所帮助,例如连接到 AD 等?

【问题讨论】:

    标签: c# asp.net-mvc active-directory


    【解决方案1】:

    由于您拥有username,您可以查询Active Directory 并获取用户所需的必要信息。这是使用 AccountManagement 命名空间对 AD 的简单调用。

    ...
    string UserName = "yourUserName";
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com")
    {
       using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName)
       if (user != null)
       {
          //user will contain information such as name, email, phone etc..
       }
    }
    

    【讨论】:

    • 是否需要用户名密码才能查询活动目录?
    • 这取决于您的应用程序的运行方式。如果您在 IIS 中以特定用户运行它并且该用户有权访问 AD,则您无需指定用户名和密码。但是,假设您正在从您的开发机器连接到某个远程 AD 服务器,那么如果您的用户名无权访问,则需要指定用户名和密码。
    • 您的示例未显示如何验证密码。
    • ctx.ValidateCredentials(username, password, ContextOptions.Negotiate) ... 以防有人需要
    • @Mike ,它没有显示如何进行身份验证的原因是因为问题没有要求。问题为“经过身份验证的”用户指定。
    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多