Glenn Rodriguez
Glenn Rodriguez

Reputation: 71

C# PrincipalContext Error "Server names cannot contain a space character"

I am getting the following error

'Server names cannot contain a space character' when using PrincipalContext to obtain information of a domain user.

This code works locally on my machine but craps out when I load it up on the Intranet web server.

            //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;

Upvotes: 5

Views: 5687

Answers (2)

Iman
Iman

Reputation: 18946

for me it was because of below setting of PrincipalContext.

and it was resolved after changing idenity of Application pool from ApplicationPoolIdenity to LocalSystem .

it is not a real solution but may help somebody.

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

Upvotes: 4

varsha
varsha

Reputation: 1620

This happens because If there is no container specified, the principal context class will create a System.DirectoryServices.DirectoryEntry object by binding to builtin CN=Users container to start searching for users. System,

consider the example on this link and about the exception page .

Upvotes: 1

Related Questions