简体   繁体   English

来自Active Directory的UserPrincipal

[英]UserPrincipal from Active Directory

I have problem with getting UserPrincipal from Active Directory. 从Active Directory获取UserPrincipal时遇到问题。 First of all I have used on my local environment (using not IIS but ASP.NET development Server): 首先,我在我的本地环境中使用(不使用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;

And it works fine. 它工作正常。 I got what I want. 我得到了我想要的东西。 But when I install application on testing environment I got error "Object reference not set to an instance of an object". 但是当我在测试环境中安装应用程序时,我收到错误“对象引用没有设置为对象的实例”。 I have tried solution from here . 我从这里尝试过解决方案。

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

But it does not work. 但它不起作用。

I use windows authentication. 我使用Windows身份验证。 Impersonation is set to true. 模拟设置为true。 Please help me. 请帮我。

change the identity of your ApplicationPool to run using domain user. 更改ApplicationPool的标识以使用域用户运行。

in iis 6 right-click your application pool, go to Identity tab and set a domain user under which the pool will run. 在iis 6中右键单击您的应用程序池,转到“ Identity选项卡并设置将在其下运行池的域用户。

in iis 7 right-click your application pool, select advance settings, under process model you'll find Identity , change it to use domain user. 在iis 7中右键单击您的应用程序池,选择高级设置,在进程模型下您将找到Identity ,将其更改为使用域用户。

you can also pass a domain user and pass to PrincipalContest Constructor 您也可以传递域用户并传递给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;
}

if your domain name is dom.com then your container would be something like DC=dom,DC=com and the user name should be given as user@dom.com or dom\\user\u003c/code> 如果你的域名是dom.com那么你的容器就像DC=dom,DC=com ,用户名应该是user@dom.comdom\\user\u003c/code>

Use this: 用这个:

 // 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);
        }

You must wrap any 'context' calls in HostingEnvironment.Impersonate 您必须在HostingEnvironment.Impersonate包装任何“上下文”调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Active Directory UserPrincipal问题 - Active Directory UserPrincipal issue 给定一个UserPrincipal对象,如何从Active Directory中获取“公司”和“部门”? - How to get “Company” and “Department” from Active Directory given a UserPrincipal object? UserPrincipal对象,Active Directory查询:DirectoryServicesCOMException - UserPrincipal Object, Active Directory Query: DirectoryServicesCOMException C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf 错误 - C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf error 我可以添加要在UserPrincipal中加载的Active Directory属性吗? - Can I add Active Directory properties to be loaded in UserPrincipal? 如何通过扩展UserPrincipal类在Active Directory中创建联系人? - How to create a Contact in Active Directory by extending UserPrincipal class? 在 UserPrincipal 上调用 Save function 时将用户添加到 Active Directory 异常 - Add user to Active Directory Exception while calling Save function on UserPrincipal 如何获取 UserPrincipal 类未表示的 Active Directory 属性 - How to get Active Directory Attributes not represented by the UserPrincipal class 如何使用 C# 中的 UserPrincipal 获取 Active Directory 中的地址详细信息 - How to get details of address in Active Directory using UserPrincipal in C# 使用PrincipalContext搜索Active Directory组时,不会返回某些UserPrincipal属性 - When Searching Active Directory Groups Using PrincipalContext Certain UserPrincipal Properties are not Returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM