I have the following code which is called inside of an ASP.NET application:
public DomainUserInfo GetDomainUserInfoByName(string domain, string firstName, string lastName)
{
string[] domainArray = domain.Split(',');
foreach (string d in domainArray)
{
var principalContext = new PrincipalContext(ContextType.Domain, d);
var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};
using (var searcher = new PrincipalSearcher(userPrincipal))
{
userPrincipal = (UserPrincipal) searcher.FindOne();
}
if (userPrincipal != null)
{
var domainUserInfo = new DomainUserInfo
{
FirstName = userPrincipal.GivenName,
LastName = userPrincipal.Surname,
Email = userPrincipal.EmailAddress,
LanID = userPrincipal.SamAccountName,
Extension = userPrincipal.VoiceTelephoneNumber,
DomainName = d,
NTAccountName = userPrincipal.Sid.Translate(typeof (NTAccount)).ToString()
};
return domainUserInfo;
}
}
return null;
}
It works when deployed on some servers but not on others, where it throws the exception:
[COMException (0x80005000): Unknown error (0x80005000)]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +386081
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.PropertyValueCollection.PopulateList() +21
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +49
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +135
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +288
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +118
System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) +34
System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() +37
System.DirectoryServices.AccountManagement.UserPrincipal.set_GivenName(String value) +17
Mfc.Inv.RM.Framework.ActiveDirectory.ActiveDirectoryManager.GetDomainUserInfoByName(String domain, String firstName, String lastName) +167
It looks like this is occurring on the line:
var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};
when trying to set the GivenName
property of the UserPrincipal
object.
I'm totally stuck as to what could be causing this, especially since it works on some servers and not others. I already tried writing a console application that calls the same code it works on all of the servers, so I am guessing it has to be something to do with IIS.
here is what I am doing and if you were to hover over userFind
or do a QuickWatch on it you will see the following information. also notice the IdentityType.SamAccountName that I am passing
var pc = new PrincipalContext(ContextType.Domain, domainName, null, null);
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);
User contributions licensed under CC BY-SA 3.0