从用户普通对象获取netbiosname
-
28-09-2019 - |
题
我正在使用System.DirectoryServices.AccountManagement部分.NET库的部分将接口到ActiveDirectory。
在groupprincipal对象上调用getMembers()并过滤结果后,我现在有了一个userprincipal对象的集合
GroupPrincipal myGroup; // population of this object omitted here
foreach (UserPrincipal user in myGroup.GetMembers(false).OfType<UserPrincipal>())
{
Console.WriteLine(user.SamAccountName);
}
上面的代码示例将打印出诸如“ Tastuser1”之类的用户名。我需要将它们与来自“ domain testuser1”格式中另一个应用程序的列表进行比较。
如何从用户主体对象获得“域”部分?
我不能仅仅附加已知的域名,因为涉及多个域,我需要区分域1 testuser1和domain2 testuser2。
解决方案
您有两个我能想到的选择。
- 解析或拿走一切的权利
name@fully.qualified.domain.name
; - 使用
System.DirectoryServices
名称空间。
我不知道 用户普通, ,我也没有 Groupprincipal. 。另一方面,我知道一种工作方式来实现您想要的东西。
[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())
}
此问题中可用的其他相关信息或链接。
C#Active Directory:获取用户的域名?
如何找到域名的NetBios名称
其他提示
使用Activeds com库,它具有可行的内置名称翻译,并且没有任何假设(如此处的其他答案)。
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);
}
}
}
您是否尝试将完全合格的域名传递给另一个应用程序?如果您这样做,大多数Windows API不会抱怨 fully_qualified_domain\USER
.
您可以在用户中查找可能的域。域1中的用户应包含字符串“ dc = domain1”。它绝对不应包含字符串“ DC = domain2”。
正如我认为这是最近的一个好答案的评论中提到的那样:
user.Sid.Translate(typeof(System.Security.Principal.NTAccount)).ToString()
不隶属于 StackOverflow