从UserPrincipal对象获取NETBIOSNAME [英] Get nETBIOSName from a UserPrincipal object

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

问题描述

我使用.net库的System.DirectoryServices.AccountManagement部分的接口到ActiveDirectory中。

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

有叫GetMembers()在GroupPrincipal对象和过滤结果,我现在有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);
}

以上code样品将打印出像TestUser1用户名。我需要这些比较列表从DOMAIN \ TestUser1格式的另一个应用程序来了。

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

我如何获得域部分从UserPrincipal对象?

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

我不能只追加一个已知域名,因为涉及到多个域,我需要区分DOMAIN1 \ TestUser1和DOMAIN2 \ TestUser2。

I can't just append a known domain name as there are multiple domains involved and I need to differentiate DOMAIN1\TestUser1 and DOMAIN2\TestUser2.

推荐答案

您有两个选择,我能想到的。

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

等相关信息或链接在此等问题可用。
<一href="http://stackoverflow.com/questions/4249139/c-active-directory-get-domain-name-of-user/4249412#4249412">C#活动目录:获取用户域名
如何找到一个域的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆