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 :
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainControllerService.GetDomain());
with stack trace :
<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 :
public ActionResult Login()
{
var model = new DomainControllerViewModel();
return View(model);
}
[HttpPost]
public ActionResult Login(DomainControllerViewModel model)
{
try
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MYDOMAIN.CORP");
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, model.Username);
if (user != null)
{
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 :
[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) :
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 :
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 :
<pre>public static string RemoveWhitespace(string input)
{
return new string(input.ToCharArray()
.Where(c => !Char.IsWhiteSpace(c))
.ToArray());
}