Asp.net UserPrincipal.FindByIdentity在IIS服务器上返回null

Asp.net UserPrincipal.FindByIdentity在IIS服务器上返回null,asp.net,directoryservices,Asp.net,Directoryservices,我在ASP.NET中有以下代码示例 using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain)) { using (UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, HttpContext.Current.User.Identity.Name))

我在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

它在您的开发设备上运行而不是在产品设备上运行的原因是,在您的开发设备上,网站在您的网络ID下运行,该ID具有域权限,但在生产中,它是在网络服务下运行的,网络服务对您的域没有任何权限。您可以:

  • 将运行IIS应用程序池的帐户更改为域帐户
  • 将模拟部分添加到web.config文件中,其中帐户是域帐户
  • 在PrincipalContext中明确指定用户名/密码,用于向域进行身份验证