加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:46:39 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET中有以下代码示例 using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain)) { using (UserPrincipal user = UserPrincipal.FindByIdentity(domainContext,HttpContext.Current.User.Identity.Name)) { if (user == n
我在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,在网络服务下运行应用程序池,我关闭了匿名访问.但是代码无法获取UserPrincipal对象.

我是否必须更改应用程序池以在域帐户下运行才能获得UserPrincipal?

解决方法

它在您的开发箱上而不是在您的产品盒上工作的原因是,在您的开发箱上,网站运行在您的网络ID下,该网络ID具有域名权限,但在生产中它运行在网络服务下,该域名对您的域名没有任何权利.你可以:

>将IIS App Pool运行的帐户更改为域1>在您的web.config文件中添加模拟部分,其中该帐户是域帐户>在PrincipalContext中明确指定用户名/密码,用于对域进行身份验证.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读