【问题标题】:C# PrincipalContext Error "Server names cannot contain a space character"C# PrincipalContext 错误“服务器名称不能包含空格字符”
【发布时间】:2016-12-03 19:33:22
【问题描述】:

我收到以下错误

使用PrincipalContext获取域用户信息时,“服务器名称不能包含空格字符”。

这段代码在我的机器上本地运行,但是当我将它加载到 Intranet Web 服务器上时,它就会出错。

            //GET CURRENT USER 
            String winUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            string[] domainUsername = winUser.Split(Convert.ToChar(@"\"));

            // set up domain context
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainUsername[0]);

            // find a user
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, domainUsername[1]);

            return user;

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    对我来说这是因为 PrincipalContext 的以下设置。

    在将应用程序池的身份从 ApplicationPoolIdenity 更改为 LocalSystem 后解决。

    这不是一个真正的解决方案,但可能会对某人有所帮助。

    PrincipalContext pc = new PrincipalContext((Environment.UserDomainName == Environment.MachineName ?
                ContextType.Machine : ContextType.Domain), Environment.UserDomainName);
    

    【讨论】:

      【解决方案2】:

      这是因为如果没有指定容器,主体上下文类将通过绑定到内置 CN=Users 容器来创建 System.DirectoryServices.DirectoryEntry 对象以开始搜索用户。系统,

      考虑这个link 上的示例以及关于异常page 的示例。

      【讨论】:

      • 我尝试在这段代码中指定一个容器:PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainUsername[0], "DC=com");同样,它在我的本地机器上运行,但不在 iis 服务器上
      • 是的,我尝试指定一个容器,就像您提供的链接中显示的示例一样
      • 错误提示你没有指定容器。可以使用 PrincipalContext(ContextType, String, String) 方法来初始化实例
      • 再次检查数据类型
      猜你喜欢
      • 2015-08-10
      • 2013-08-31
      • 1970-01-01
      • 2011-09-30
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2019-06-02
      相关资源
      最近更新 更多