0

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.

8
  • are you familiar with PrincipalCOntext..? if so why do you need to do the following var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName}; you should be able to get at all the necessary information just using PrincipalContext also the reason it may not work on other servers is because the other servers may not be in the same Domain or AD Tree. get with your Admin as well
    – MethodMan
    Sep 3, 2014 at 15:36
  • @DJKRAZE I did not write the code. Also, PrincipalContext does not have properties for GivenName or Surname, or EmailAddress (which we use to search in a different method)
    – mclaassen
    Sep 3, 2014 at 15:38
  • funny.. because I am looking at some code right now that I can see that.. let me clarify you do not need to use the new UserPrincipal I will show you what I am doing and what I mean and it works very well..
    – MethodMan
    Sep 3, 2014 at 15:40
  • On PrincipalContext class? All I see is Name and UserName
    – mclaassen
    Sep 3, 2014 at 15:42
  • @DJKRAZE Also, just verified all the servers are members of the same (correct) domain.
    – mclaassen
    Sep 3, 2014 at 15:50

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.