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

后端 未结 4 1313
天命终不由人
天命终不由人 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:17

    The "pre Windows 2000" name i.e. DOMAIN\SomeBody, the Somebody portion is known as sAMAccountName.

    So try:

    using(DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainController"))
    {
       using(DirectorySearcher adSearch = new DirectorySearcher(de))
       {
         adSearch.Filter = "(sAMAccountName=someuser)";
         SearchResult adSearchResult = adSearch.FindOne();
       }
    }
    

    someuser@somedomain.com.au is the UserPrincipalName, but it isn't a required field.

提交回复
热议问题