[英]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.com
或dom\\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.