UserPrincipal.FindByIdentity 在 IIS 服务器上返回 null
我在 ASP.NET 中有以下代码示例
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, HttpContext.Current.User.Identity.Name))
{
if (user == null)
{
lbName.Text = "No User Principal";
}
else
{
lbName.Text = user.DisplayName;
}
}
}
web.config 看起来像
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
我在本地开发计算机上尝试了代码(域的一部分,以域用户身份登录,VS2010,.Net 4.0,Windowx XP)在本地进行测试,我能够获取 UserPrincipal 对象。
如果我部署到 WIndows 2003(也是域的一部分)、IIS6、.Net 4.0,并在网络服务下运行应用程序池,我会关闭匿名访问。但代码无法获取 UserPrincipal 对象。
我是否必须将应用程序池更改为在域帐户下运行才能获取 UserPrincipal
?
I have following code sample in ASP.NET
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, HttpContext.Current.User.Identity.Name))
{
if (user == null)
{
lbName.Text = "No User Principal";
}
else
{
lbName.Text = user.DisplayName;
}
}
}
The web.config looks like
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
I tried the code on my local development machine (part of domain, logon as domain user, VS2010, .Net 4.0, Windowx XP) to test locally, I am able to get UserPrincipal
object.
If I deploy to WIndows 2003 (also part of the domain), IIS6, .Net 4.0 with application pool running under Network Service, I turned off anonymous access. But the code is not able to get UserPrincipal
object.
Do I have to change application pool to run under a domain account in order to get UserPrincipal
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在您的开发盒上工作而不是在您的产品盒上工作的原因是,在您的开发盒上,网站在您的网络 ID 下运行,该网络 ID 具有域权限,但在生产中,它在网络服务下运行,该网络服务对您的域没有权限。您可以:
The reason it worked on your dev box and not on your prod box is that on your dev box the website ran under your network ID, which had domain rights, but in production it's running under network service which has no rights to your domain. You can either: