我在从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;它工作得很好。我得到了我想要的。但是当我在测试环境中安装应用程序时,我得到了错误"Object reference not set to an instance of an object“。我已经尝试过来自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身份验证。模拟设置为true。请帮帮我。
发布于 2012-10-02 23:37:30
将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.com或dom\user
发布于 2016-01-23 00:47:00
使用以下命令:
// 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”调用
https://stackoverflow.com/questions/12692761
复制相似问题