首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自Active Directory的UserPrincipal

来自Active Directory的UserPrincipal
EN

Stack Overflow用户
提问于 2012-10-02 22:54:39
回答 2查看 13.6K关注 0票数 2

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

代码语言:javascript
复制
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;

它工作得很好。我得到了我想要的。但是当我在测试环境中安装应用程序时,我得到了错误"Object reference not set to an instance of an object“。我已经尝试过来自here的解决方案。

代码语言:javascript
复制
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, usr.SoeId);
    return up.DisplayName;
    // or return up.GivenName + " " + up.Surname;
}

但它不起作用。

我使用windows身份验证。模拟设置为true。请帮帮我。

EN

回答 2

Stack Overflow用户

发布于 2012-10-02 23:37:30

ApplicationPool的标识更改为使用域用户运行。

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

在iis 7中,右键单击您的应用程序池,选择高级设置,在进程模型下找到Identity,将其更改为使用域用户。

您还可以传递域用户并传递给PrincipalContest Constructor

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2016-01-23 00:47:00

使用以下命令:

代码语言:javascript
复制
 // 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中包装任何“context”调用

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12692761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档