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

后端 未结 4 1310
天命终不由人
天命终不由人 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 21:35

    If you're using .NET 3.5 SP1+ the better way to do this is to take a look at the

    System.DirectoryServices.AccountManagement namespace.
    

    It has methods to find people and you can pretty much pass in any username format you want and then returns back most of the basic information you would need. If you need help on loading the more complex objects and properties check out the source code for http://umanage.codeplex.com its got it all.

    Brent

    0 讨论(0)
  • 2020-11-30 21:36

    Add reference to COM "Active DS Type Library"


                Int32 nameTypeNT4               = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4;
                Int32 nameTypeDN                = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_1779;
                Int32 nameTypeUserPrincipalName = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME;
    
                ActiveDs.NameTranslate nameTranslate = new ActiveDs.NameTranslate();
    
                // Convert NT name DOMAIN\User into AD distinguished name 
                // "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com"
                nameTranslate.Set(nameTypeNT4, ntUser);
    
                String distinguishedName = nameTranslate.Get(nameTypeDN);
    
                Console.WriteLine(distinguishedName);
    
                // Convert AD distinguished name "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com" 
                // into NT name DOMAIN\User
                ntUser = String.Empty;
                nameTranslate.Set(nameTypeDN, distinguishedName);
                ntUser = nameTranslate.Get(nameTypeNT4);
                Console.WriteLine(ntUser);
    
                // Convert NT name DOMAIN\User into AD UserPrincipalName Name.User@Company.com
                nameTranslate.Set(nameTypeNT4, ntUser);
                String userPrincipalName = nameTranslate.Get(nameTypeUserPrincipalName);
    
                Console.WriteLine(userPrincipalName);
    
    0 讨论(0)
提交回复
热议问题