I'm trying to validate username and password against the ActiveDirectory using the following code.
public bool GetActiveDirectoryUser(string domainName, string userName, string password)
{
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName, userName, password);
Above mentioned code was working perfectly on my local system, while trying to login with local domain user.
But when I tried to access other remote domain. It started giving following error:
"The server could not be contacted."
"System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable. "
So, I modified my code as following & started accessing remote Domain also.
public bool GetActiveDirectoryUser(string domainName, string userName, string password)
{
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName);
But when pushed these changes on my test server, where it is running under SSL on IIS 7.0.
Same error starts coming again i.e. for my local domain it validates all users perfectly but for other domain it gives error.
Following is details of error:
Message:
The server could not be contacted.
Inner Exception:
System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.
at System.DirectoryServices.Protocols.LdapConnection.Connect()
at System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest request, Int32& messageID)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
Stack Trace:
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)
I've developed this using Visual Studio 2008, Framework 3.5
I'm passing domain information in follwoing format: 'mylocaldomain'
Can anybody help me, why this is happening & how it can be resolved.
Thanks in advance.
gratisaccoun...
Member
77 Points
42 Posts
Not Validationg Remote Domain User Via Active Directory
Mar 06, 2012 05:52 PM|LINK
Dear All,
I'm trying to validate username and password against the ActiveDirectory using the following code.
public bool GetActiveDirectoryUser(string domainName, string userName, string password)
{
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName, userName, password);
UserPrincipal user = UserPrincipal.FindByIdentity(pc, userName);
UserInfo objUserInfo;//This is struct
if (user != null)
{
objUserInfo = new UserInfo();
objUserInfo.userName = user.SamAccountName;
objUserInfo.firstName = user.GivenName;
objUserInfo.middleName = user.MiddleName;
objUserInfo.lastName = user.Surname;
objUserInfo.welcomeName = user.Name;
objUserInfo.projectList = null;
return true;
}
return false;
}
catch (Exception ex)
{
//Exception Handling code Goes Here.
}
return false;
}
Above mentioned code was working perfectly on my local system, while trying to login with local domain user.
But when I tried to access other remote domain. It started giving following error:
"The server could not be contacted."
"System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable. "
So, I modified my code as following & started accessing remote Domain also.
public bool GetActiveDirectoryUser(string domainName, string userName, string password)
{
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName);
if (pc.ValidateCredentials(userName, password))
{
UserPrincipal user = UserPrincipal.FindByIdentity(pc, userName);
UserInfo objUserInfo;
if (user != null)
{
objUserInfo = new UserInfo();
objUserInfo.userName = user.SamAccountName;
objUserInfo.firstName = user.GivenName;
objUserInfo.middleName = user.MiddleName;
objUserInfo.lastName = user.Surname;
objUserInfo.welcomeName = user.Name;
objUserInfo.projectList = null;
return true;
}
return false;
}
else
{
ErrorMessage = "Invalid username/password";
return false;
}
}
catch (Exception ex)
{
//Exception Handling code Goes Here.
}
return false;
}
But when pushed these changes on my test server, where it is running under SSL on IIS 7.0.
Same error starts coming again i.e. for my local domain it validates all users perfectly but for other domain it gives error.
Following is details of error:
Message:
The server could not be contacted.
Inner Exception:
System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.
at System.DirectoryServices.Protocols.LdapConnection.Connect()
at System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest request, Int32& messageID)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
Stack Trace:
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)
I've developed this using Visual Studio 2008, Framework 3.5
I'm passing domain information in follwoing format: 'mylocaldomain'
Can anybody help me, why this is happening & how it can be resolved.
Thanks in advance.