Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a MVC5 application whereby the user has to login using domain credentials. When i hardcode the values for domain variables the application logs in without issue. For security reason i have set the variable values in a database table but when i attempt to retrieve using Linq i get an error Server names cannot contain a space character at this line :

C#
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainControllerService.GetDomain());


with stack trace :
C#
<pre> at System.DirectoryServices.Protocols.LdapDirectoryIdentifier..ctor(String[] servers, Boolean fullyQualifiedDnsHostName, Boolean connectionless)
   at System.DirectoryServices.Protocols.LdapDirectoryIdentifier..ctor(String server)
   at System.DirectoryServices.Protocols.LdapConnection..ctor(String server)
   at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name)
   at FBChecklist.Controllers.UserController.Login(DomainControllerViewModel model) in C:\Users\tshumae.FBC\source\repos\FBCHECKLIST\FBChecklist\Controllers\UserController.cs:line 49


This is the login code using hardcoded values :
C#
public ActionResult Login()
        {
            var model = new DomainControllerViewModel();
           // LoginViewModel model = new LoginViewModel();
            return View(model);
        }

        // POST: User/Delete/5
        [HttpPost]
        public ActionResult Login(DomainControllerViewModel model)
        {
            try
            {
              PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MYDOMAIN.CORP");              
                // find a user
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, model.Username);
                if (user != null)
                {
                    // check user lockout state
                    if (user.IsAccountLockedOut())
                    {
                        ViewBag.Message = "Your account is locked out";
                    }
                    else
                    {
                        bool authentic = false;
                        try
                        {
                            DirectoryEntry entry = new DirectoryEntry("LDAP://XX.XX.XX.XX:XXX/OU=YYY,DC=YYY,DC=corp",, model.Username, model.Password);
                            DirectoryEntry ldapConnection = new DirectoryEntry("MYDOMAIN.CORP");
                            ldapConnection.Path = "LDAP://";
                            ldapConnection.Username ="myusername";
                            ldapConnection.Password = "mypassword";
                            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
....
....
               
            return View();
        }


And for retrieving from db im using :

C#
 [HttpPost]
        public ActionResult Login(DomainControllerViewModel model)
        {
            try
            {
               PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainControllerService.GetDomain());
  ...
...                           
                        try
                        {

                            DirectoryEntry entry = new DirectoryEntry(domainControllerService.GetDirectoryEntry(), model.Username, model.Password);
                            DirectoryEntry ldapConnection = new DirectoryEntry(domainControllerService.GetDomain());
                            ldapConnection.Path = "LDAP://";
                            ldapConnection.Username =domainControllerService.GetUsername();
                            ldapConnection.Password = domainControllerService.GetPassword();
                            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
...
...
return View();
}


Repository (DomainControllerService.cs) :
C#
public string GetDomain()
       {
           var domain = (from j in appEntities.DomainControllers
                         select new
                         {
                             j.Domain
                         });
           return domain.ToString();
       }


What I have tried:

I have tried a custom extension method that removes whitespace from a string as below but i still keep getting the same exception :

C#
public string GetDomain()
       {
           var domain = (from j in appEntities.DomainControllers
                         select new
                         {
                             j.Domain
                         });
           string domaintostring = domain.ToString();
           string dom = Helpers.RemoveWhitespace(domaintostring);
           return dom;
       }


and the method :
C#
<pre>public static string RemoveWhitespace(string input)
        {
            return new string(input.ToCharArray()
                .Where(c => !Char.IsWhiteSpace(c))
                .ToArray());
        }
Posted
Updated 28-May-20 2:44am
Comments
phil.o 28-May-20 7:56am    
Your only option is to debug your code and investigate the value of the variable holding the server name.

1 solution

As mentioned in a comment, you should debug your code and step through line-by-line and investigate the variables to see what values are being produced.

That being said, looking at the below:
C#
var domain = (from j in appEntities.DomainControllers
              select new
              {
                  j.Domain
              });
return domain.ToString();

This appears to be creating an IEnumerable<string> and you're then calling ToString() on the enumerable. This might be a good place to start.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900