从 UserPrincipal 对象中获取 nETBIOSName

Get nETBIOSName from a UserPrincipal object(从 UserPrincipal 对象中获取 nETBIOSName)

本文介绍了从 UserPrincipal 对象中获取 nETBIOSName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .Net 库的 System.DirectoryServices.AccountManagement 部分来连接到 ActiveDirectory.

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

在 GroupPrincipal 对象上调用 GetMembers() 并过滤结果后,我现在有一组 UserPrincipal 对象

Having called GetMembers() on a GroupPrincipal object and filter the results, I now have a collection of UserPrincipal objects

GroupPrincipal myGroup;  // population of this object omitted here 

foreach (UserPrincipal user in myGroup.GetMembers(false).OfType<UserPrincipal>())
{
    Console.WriteLine(user.SamAccountName);
}

上面的代码示例将打印出像TestUser1"这样的用户名.我需要将这些与来自DOMAINTestUser1"格式的另一个应用程序的列表进行比较.

The above code sample will print out usernames like "TestUser1". I need to compare these to a list coming from another application in "DOMAINTestUser1" format.

如何从 UserPrincipal 对象中获取DOMAIN"部分?

How do I get the "DOMAIN" part from the UserPrincipal object?

我不能只附加一个已知域名,因为涉及多个域,我需要区分 DOMAIN1TestUser1 和 DOMAIN2TestUser2.

I can't just append a known domain name as there are multiple domains involved and I need to differentiate DOMAIN1TestUser1 and DOMAIN2TestUser2.

推荐答案

你有两个我能想到的选择.

You have two choices that I can think of.

  1. 解析或获取name@fully.qualified.domain.name右侧的所有内容;
  2. 使用 System.DirectoryServices 命名空间.
  1. Parse, or take everything that is on, the right of name@fully.qualified.domain.name;
  2. Use the System.DirectoryServices namespace.

我不了解 UserPrincipal,也不了解 GroupPrincipal.另一方面,我知道一种可行的方法来实现你想要的.

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("INTRATESTUSER1", string.Concat(netBiosName, "", foundLogin).ToUpperInvariant())
}

此 SO 问题中提供的其他相关信息或链接.
C# Active Directory:获取用户的域名?
如何查找域的 NetBIOS 名称

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

这篇关于从 UserPrincipal 对象中获取 nETBIOSName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 UserPrincipal 对象中获取 nETBIOSName

基础教程推荐