简体   繁体   中英

Get fullname from Active Directory on C#

I have a problem about getting user's firstname and surname (fullname) from Active Directory. The code is below.

No problem with runing the project on my local. I can take data whatever I want. After publishing the project to remote server, it don't allow me to get firstname and surname (fullname), it just allows to get domain name and username. How can I fix it?

private string GetNameSurname()
    {
        string[] retVal;
        //Pull the username out of the domain\user string.  
        retVal = Page.User.Identity.Name.Split(new string[] { @"\"},StringSplitOptions.RemoveEmptyEntries);

        DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + retVal[0] + "/" + retVal[1]);
        string namesurname = (string)userEntry.Properties["fullname"].Value;
        //return retVal[1];
        //retVal[1] gives username. 
        //retVal[0] gives domain name
        return namesurname;
    }

Are you running the code to test for the same user?

Are you testing against AD in dev, Windows local accounts have a Full Name field (optional) but in AD the fields are named differently. Also, are you talking to the same version of AD.

According to the MSDN article there is no field called Full Name, only First Name and Surname and Display Name.

I think your problem is about credentials. Try to add an administrator username and password when conecting to active directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM