Get nETBIOSName from a UserPrincipal object

后端 未结 5 1433
-上瘾入骨i
-上瘾入骨i 2021-01-02 01:13

I am using the System.DirectoryServices.AccountManagement part of the .Net library to interface into ActiveDirectory.

Having called GetMembers() on a GroupPrincipal

相关标签:
5条回答
  • 2021-01-02 01:49

    As mentioned in one of the comments to the question I think this is a good answer for more recent times:

     user.Sid.Translate(typeof(System.Security.Principal.NTAccount)).ToString()
    
    0 讨论(0)
  • 2021-01-02 01:52

    Have you tried passing the fully qualified domain name to this other app? Most windows API's won't complain if you do fully_qualified_domain\USER.

    0 讨论(0)
  • 2021-01-02 01:54

    You could look for the possible domains in the user.DistinguishedName property. A user in Domain 1 should contain the string "DC=DOMAIN1". It definitely shouldn't contain the string "DC=DOMAIN2".

    0 讨论(0)
  • 2021-01-02 02:01

    Use the ActiveDs COM library, it has built-in name translation that works and does not make any assumptions (like other answers here).

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ActiveDs;
    
    namespace Foo.Repository.AdUserProfile
    {
        public class ADUserProfileValueTranslate
        {
            public static string ConvertUserPrincipalNameToNetBiosName(string userPrincipleName)
            {
                NameTranslate nameTranslate = new NameTranslate();
                nameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME, userPrincipleName);
                return nameTranslate.Get((int) ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 02:02

    You have two choices that I can think of.

    1. Parse, or take everything that is on, the right of name@fully.qualified.domain.name;
    2. Use the System.DirectoryServices namespace.

    I don't know about UserPrincipal, neither do I about GroupPrincipal. On the other hand, I know of a working way to achive to what you want.

    [TestCase("LDAP://fully.qualified.domain.name", "TestUser1")] 
    public void GetNetBiosName(string ldapUrl, string login)
        string netBiosName = null;
        string foundLogin = null;
    
        using (DirectoryEntry root = new DirectoryEntry(ldapUrl))
            Using (DirectorySearcher searcher = new DirectorySearcher(root) {
                searcher.SearchScope = SearchScope.Subtree;
                searcher.PropertiesToLoad.Add("sAMAccountName");
                searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", login);
    
                SearchResult result = null;
    
                try {
                    result = searcher.FindOne();
    
                    if (result == null) 
                        if (string.Equals(login, result.GetDirectoryEntry().Properties("sAMAccountName").Value)) 
                            foundLogin = result.GetDirectoryEntry().Properties("sAMAccountName").Value
                } finally {
                    searcher.Dispose();
                    root.Dispose();
                    if (result != null) result = null;
                }
            }
    
        if (!string.IsNullOrEmpty(foundLogin)) 
            using (DirectoryEntry root = new DirectoryEntry(ldapUrl.Insert(7, "CN=Partitions,CN=Configuration,DC=").Replace(".", ",DC=")) 
                Using DirectorySearcher searcher = new DirectorySearcher(root)
                    searcher.Filter = "nETBIOSName=*";
                    searcher.PropertiesToLoad.Add("cn");
    
                    SearchResultCollection results = null;
    
                    try {
                        results = searcher.FindAll();
    
                        if (results != null && results.Count > 0 && results[0] != null) {
                            ResultPropertyValueCollection values = results[0].Properties("cn");
                            netBiosName = rpvc[0].ToString();
                    } finally {
                        searcher.Dispose();
                        root.Dispose();
    
                        if (results != null) {
                            results.Dispose();
                            results = null;
                        }
                    }
                }
    
        Assert.AreEqual("INTRA\TESTUSER1", string.Concat(netBiosName, "\", foundLogin).ToUpperInvariant())
    }
    

    Other related information or links available in this SO question.
    C# Active Directory: Get domain name of user?
    How to find the NetBIOS name of a domain

    0 讨论(0)
提交回复
热议问题