What is the AD property name to get DomainName\UserName which is used to log in? RRS feed

  • Question

  • User750184344 posted

    Hi, I am getting user's information from Active Directory by his entered Email Address.

    Now, I want to know what is his log in credentials, I mean like Identity.Name which displays Domain\UserName.

    Can someone tell me what is the return attribute name that I can add in my function to get the DomainName\UserName for given Email Address?

    Just for information, here is my basic function that I am using to get information about user from Active Directory and I want to add this attribute\property to get his domainname\username.

            public bool IsAuthenticated_CheckWithEmail(
                    string email
                DirectoryEntry entry = new DirectoryEntry();
                    entry = new DirectoryEntry(_path);
                    // Bind to the native AdsObject to force authentication.
                    Object obj = entry.NativeObject;
                    DirectorySearcher search = new DirectorySearcher(entry);
                    search.Filter = "(mail=" + email + ")";
                    search.PropertiesToLoad.Add("cn"); // Login Name
                    search.PropertiesToLoad.Add("givenName"); // First Name
                    search.PropertiesToLoad.Add("sn"); // Last Name
                    search.PropertiesToLoad.Add("displayName"); // Display Name
                    search.PropertiesToLoad.Add("memberof"); //Get User Groups
                    SearchResult result = search.FindOne();
                    if (null == result)
                        return false;
                    // Update the new path to the user in the directory
                    _path = result.Path;
                    _filterAttribute = (String)result.Properties["cn"][0];
                    FirstName = (String)result.Properties["givenName"][0];
                    LastName = (String)result.Properties["sn"][0];
                    EmailAddress = (String)result.Properties["mail"][0];
                    //TODO: find out attribute name for this to get domain/username
                    //UserName = (String)result.Properties["cn"][0];    
                    UserDisplayName = (String)result.Properties["displayName"][0];
                    TelephoneNumber = (String)result.Properties["TelephoneNumber"][0];
                catch (Exception ex)
                    throw new Exception("Login Error: " + ex.Message);
                return true;



    Tuesday, January 12, 2010 9:56 AM

All replies

  • User-110466603 posted

    Try samAccountName

    I don't think it puts the domain name in front of it (i..e, DOMAIN\samAccountName).  Here is a blog I wrote on querying AD from SQL 2005. I am posting this because there are field names in my post so it may help you out.


    Tuesday, January 12, 2010 1:14 PM
  • User750184344 posted

    I tried samAccountName but it does not include domain name.

    I am looking for the prpperty name which displays "DomainName\SamAccountName".


    Tuesday, January 12, 2010 2:48 PM
  • User-110466603 posted

    Ok, I didn't think there was a property that included the domain name, just the login name. I'd be curious to see what other say or find.  Is there a reason you don't want to append that part of the string since you need that value to do your AD search anyway?

    Here is a link I found as well, but it doesn't look like it provided a full solution. However there is a link in that thread pointing on how to grab the netBIOS name of the domain.


    Tuesday, January 12, 2010 3:37 PM
  • User750184344 posted

    I am extreme sorry for late reply.

    ok, I can use SAMAccountName and can append it but then the question is how do I get DomainName? I mean using which property?

    If I can get SamAccountName and Domain from different properties, I can surely combine it for my purpose.


    Wednesday, January 20, 2010 1:32 PM