C# PrincipalContext 错误 "Server names cannot contain a space character"

标签 c# .net

我收到以下错误

使用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;

最佳答案

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

并且在将应用程序池的标识从 ApplicationPoolIdenity 更改为 LocalSystem 后解决了这个问题。

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

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

关于C# PrincipalContext 错误 "Server names cannot contain a space character",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406731/

相关文章:

c# - 多线程任务读取列表,导致索引超出范围

c# - 如何在 FormBorderStyle 属性设置为 None 时移动 Windows 窗体?

c# - JSON.net(反)序列化未类型化的属性

c# - 无效方差 : The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList' . 'T' 是协变的

c# - 删除 .net 代码中的文字字符串

c# - 每次在调用任何其他方法之前调用一个方法

.net - 连接字符串中 ConnectTimeout 的默认值是多少?

c# - 将短语翻译与 String.Format 集成

.net - 在 Neo4j 中插入节点非常慢

C# 生成 Azure 表存储 ConnectionString