【问题标题】:UserPrincipal from Active Directory来自 Active Directory 的 UserPrincipal
【发布时间】:2012-10-02 14:54:38
【问题描述】:

我在从 Active Directory 获取 UserPrincipal 时遇到问题。首先,我在本地环境中使用过(不是使用 IIS,而是使用 ASP.NET 开发服务器):

User usr = new User();
usr.SoeId = Request.ServerVariables["LOGON_USER"];
usr.IP = Request.ServerVariables["REMOTE_ADDR"];
usr.FirstName = UserPrincipal.Current.GivenName;
usr.LastName = UserPrincipal.Current.Surname;

而且效果很好。我得到了我想要的。但是,当我在测试环境中安装应用程序时,出现错误“对象引用未设置为对象的实例”。我已经尝试过here 的解决方案。

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, usr.SoeId);
    return up.DisplayName;
    // or return up.GivenName + " " + up.Surname;
}

但它不起作用。

我使用 Windows 身份验证。模拟设置为真。请帮我。

【问题讨论】:

  • 请显示更多代码,而不仅仅是 UserPrincipal.Current.GivenName 我们需要查看您如何实例化 Object 变量
  • 在 IIS 中是否关闭了 "Allow anonymous"
  • 是的,匿名身份验证已禁用。如果有任何意义,我会使用 IIS 7.0 和 Windows Server 2008。
  • 是的,我遇到过这样的问题。只需仔细检查 IIS 上的安全权限是什么,以及运行您的应用程序的用户是谁:如果此用户在 AD 中没有足够的权限,您的代码将会像您所拥有的那样失败。
  • 我什至尝试以管理员身份登录,但仍然无法正常工作。 Request.ServerVariables["LOGON_USER"] 的用户名是 "DOMAIN\Administrator"

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


【解决方案1】:

ApplicationPool 的身份更改为使用域用户运行。

在 iis 6 中右键单击您的应用程序池,转到 Identity 选项卡并设置运行池的域用户。

在iis 7中右击你的应用程序池,选择高级设置,在进程模型下你会找到Identity,改成使用域用户。

您也可以传递一个域用户并传递给PrincipalContest Constructor

using (PrincipalContext context = new PrincipalContext(
                                    ContextType.Domain,
                                    "name of your domain",
                                    "container of your domain",
                                    "user@domain", //create a user in domain for context creation purpose.. this username will be constant.. you can keep it in app config
                                    "password")){
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, usr.SoeId);
    return up.DisplayName;
}

如果您的域名是dom.com,那么您的容器将类似于DC=dom,DC=com,用户名应为user@dom.comdom\user

【讨论】:

  • 我使用 IIS 7,但在 Identity DomainUser 下没有。我只能选择 LocalService、LocalSystem、NetworkService 和 ApplicationPoolIdentity(我先设置)。
  • @Kamil 是域内还是域外的 iis 服务器?
  • 您将传递PrincipalContext 一个静态用户,然后使用UserPrincipal.FindByIdentity 查找当前在现场的用户。
  • 是的,但我不确定该静态用户是否需要在管理员组中。
  • 拥有一个继承ActiveDirectoryMembershipProvider 类的自定义成员资格提供程序类。然后在你的ValidateUser 方法调用base.ValidateUser(username,password)。使用 web config 设置 LDAP 连接字符串和提供程序。
【解决方案2】:

使用这个:

 // find currently logged in user
        UserPrincipal adUser = null;
        using (HostingEnvironment.Impersonate())
        {
            var userContext = System.Web.HttpContext.Current.User.Identity;
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["AllowedDomain"], null,
                ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);
            adUser = UserPrincipal.FindByIdentity(ctx, userContext.Name);
        }

您必须在 HostingEnvironment.Impersonate 中包装任何“上下文”调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多