I am trying to get the full name of a given user from active directory. This code works on my PC but when I put it on the server it throws exception:

The network path was not found.

The code is:

DirectoryEntry obDirEntry = null;
        try
        {
            obDirEntry = new DirectoryEntry("WinNT://" + "domain" + "/" + Environment.UserName);
            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
            object obVal = coll["FullName"].Value;
            Response.Write(obVal);
        }
        catch (Exception ex)

        {
           Response.Write(ex.Message);
        }

Any idea how to fix so it would work on the server also? Or maybe some other way I can get the full name of a given user name? Do I need to use LDAP instead somehow?

Thanks!

Greg

Comments

What's the exception? Does the user your web app is running on on the server have permissions to access ActiveDirectory?

Written by robertc

The exception is: "The network path was not found." How can I check if the server has permission to access activeDirectory?

Written by Greg

Accepted Answer

There can be many issues here is what I found out

  1. Your new Directory object is pointing to WINNT I guess you need to use LDAP address for this one i.e.

    new DirectoryEntry("LDAP://" + sADServer + "/" + sNewOUPath, sADUser, sADPassword, AuthenticationTypes.Secure);

  2. In your webserver you need to change the user in your App Pool, and that user should have the proper rights to AD

  3. You can also edit your web.config to do <identity impersonate="true" /> and mage sure that the one that runs the page on the web server has the proper permission to AD

for a full implementation referenc of AD refer to this one http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

Written by Raymund
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki