How to get the current user's Active Directory details in C#

后端 未结 4 1320
天命终不由人
天命终不由人 2020-11-30 20:56

I am working on an C# and ASP.Net application, that uses Windows Authentication.

i.e. in Web.config:


    

        
4条回答
  •  醉话见心
    2020-11-30 21:16

    Alan already gave you the right answer - use the sAMAccountName to filter your user.

    I would add a recommendation on your use of DirectorySearcher - if you only want one or two pieces of information, add them into the "PropertiesToLoad" collection of the DirectorySearcher.

    Instead of retrieving the whole big user object and then picking out one or two items, this will just return exactly those bits you need.

    Sample:

    adSearch.PropertiesToLoad.Add("sn");  // surname = last name
    adSearch.PropertiesToLoad.Add("givenName");  // given (or first) name
    adSearch.PropertiesToLoad.Add("mail");  // e-mail addresse
    adSearch.PropertiesToLoad.Add("telephoneNumber");  // phone number
    

    Those are just the usual AD/LDAP property names you need to specify.

提交回复
热议问题