locked
Trouble Getting AD User Information RRS feed

  • Question

  • User-561514082 posted

    The following code returns the logged on user name if site is run on local IIS but if won't return the name if running on the server.

    What do I need to do to fix it?

            DirectoryEntry GetDirectoryEntry(string name)
            {
                string[] arr = new string[] { "sAMAccountName", "givenName", "sn", "mail" };
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + this.AD + "/dc=mydomain,dc=zz");
                DirectorySearcher searcher1 = new DirectorySearcher(entry);
                searcher1.Filter = "(&(objectClass=user)(sAMAccountName=" + name + "))";
                try
                {
                    SearchResult r = searcher1.FindOne();
                    DirectoryEntry de = r.GetDirectoryEntry();
                    return de;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    

    Tuesday, July 11, 2017 3:21 PM

Answers

  • User753101303 posted

    So if "de" is null and you don't have any exception it seems it would mean that:

    - r is not null
    - but r.GetDirectoryEntry() returns null

    My next step would be to check that assumption (which looks weird). I would expect r to be null (if the user is not found) and r.GetDirectoryEnty() to always work fine if r is not null)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 11, 2017 10:17 PM

All replies

  • User-561514082 posted

    A bit more information:

    After adding a trace log; I see that the server is generating an error "Object reference not set to an instance of an object" on line

    sUserName = de.Properties["DisplayName"][0].ToString();

    Tuesday, July 11, 2017 8:42 PM
  • User2053451246 posted

    Code runs on the server.  You can't access an end-users system in this manner in a web application.  It worked in development because your PC was the client and the server at the same time.

    Tuesday, July 11, 2017 8:46 PM
  • User753101303 posted

    Hi,

    This error means you are trying to access a member while the object is null so the usual approach is to use the debugger and check each and every object used on this line to find which one is null ie check de, de.Properties, de.Properties[0] etc... until to find which one is null.

    Not directly related but with 4.x or later you have https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.findbyidentity(v=vs.110).aspx which can ease a bit using AD.

    Edit: ah could it be just that an exception is thrown in which case you return null. So you would have to check de to see if this is null. You could also reconsider this. By catching inside this function, the caller can't get any information about the exception and it's easy to even forget that it could return null and to check for that (which is why we moved away from result code to exceptions).

    Tuesday, July 11, 2017 9:05 PM
  • User-561514082 posted

    How do I fix it?

    Tuesday, July 11, 2017 9:09 PM
  • User753101303 posted

    Make sure to fully understand the error you have before fixing it. So for now I suspect you may have an exception that you catch, causing de to be null and causing this null reference exception later.

    For now I would likely get rid of the catch unless you really expect that this code will fail often under normal circumstances. You should see which real exception you have which should help to fix the actual error.

    Tuesday, July 11, 2017 9:14 PM
  • User-561514082 posted

    I removed the Catch but didn't get any errors.

    The NULL is returned on page load where I am calling

    DirectoryEntry de = this.GetDirectoryEntry(sUserId);

    de is null

    Tuesday, July 11, 2017 9:37 PM
  • User753101303 posted

    So if "de" is null and you don't have any exception it seems it would mean that:

    - r is not null
    - but r.GetDirectoryEntry() returns null

    My next step would be to check that assumption (which looks weird). I would expect r to be null (if the user is not found) and r.GetDirectoryEnty() to always work fine if r is not null)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 11, 2017 10:17 PM
  • User-561514082 posted

    ryanbesko mentioned earlier "You can't access an end-users system in this manner in a web application" But, I have googled and found similar code used by tons of developers.

    The reason it is not returning any exception because "Catch" is also been used other places to trap the error.

    If you don't agree what ryanbesk said then I will go ahead and remove all "Catch" and see what happens.

    Tuesday, July 11, 2017 10:29 PM