![](/img/trans.png)
[英]How to get a username in Active Directory from a display name in C#?
[英]How can I search Active Directory by username using C#?
我正在嘗試使用用戶名“admin”搜索活動目錄。 我知道在目錄中有一個用戶使用該用戶名的事實,但搜索一直沒有回來。
var attributeName = "userPrincipalName";
var searchString = "admin"
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com")
var mySearcher = new DirectorySearcher(ent);
mySearcher.Filter = string.Format("(&(objectClass=user)({0}={1}))", attributeName, searchString);
var userResult = mySearcher.FindOne();
userResult總是以null結尾。 我很想知道為什么,必須有一些我不知道的東西。
如果您使用的是.NET 3.5及更高版本,則應該查看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空間。 在這里閱讀所有相關內容:
基本上,您可以定義域上下文並輕松查找AD中的用戶和/或組:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "admin");
if(user != null)
{
// do something here....
}
使用此代碼,您將通過以下屬性搜索該用戶:
DistinguishedName
:標識是專有名稱(DN)。 Guid
:身份是全球唯一標識符(GUID)。 Name
:身份是一個名稱。 SamAccountName
:標識是安全帳戶管理器(SAM)名稱。 Sid
:標識是安全描述符定義語言(SDDL)格式的安全標識符(SID)。 UserPrincipalName
:標識是用戶主體名稱(UPN)。 新的S.DS.AM使得在AD中與用戶和群組玩游戲變得非常容易!
這應該工作
private void showUsers(string pUserName)
{
string uid = Properties.Settings.Default.uid;
string pwd = Properties.Settings.Default.pwd;
using (var context = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", uid, pwd))
{
using (UserPrincipal user = new UserPrincipal(context))
{
user.SamAccountName = pUserName;
using (var searcher = new PrincipalSearcher(user))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
Console.WriteLine("SAM account name : " + de.Properties["samAccountName"].Value);
Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
Console.WriteLine("Mail: " + de.Properties["mail"].Value);
PrincipalSearchResult<Principal> groups = result.GetGroups();
foreach (Principal item in groups)
{
Console.WriteLine("Groups: {0}: {1}", item.DisplayName, item.Name);
}
Console.WriteLine();
}
}
}
}
Console.WriteLine("End");
Console.ReadLine();
}
如果您想堅持使用DirectorySearcher,請嘗試使用cn
或samaccountname
搜索
var attributeName = "cn";
var searchString = "admin"
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com")
var mySearcher = new DirectorySearcher(ent);
mySearcher.Filter = string.Format("(&(objectcategory=user)({0}={1}))", attributeName, searchString);
var userResult = mySearcher.FindOne();
事實證明,“userPrincipalName”必須全部為小寫(“userprincipalname”)。 很高興知道,感謝您的回復。
var attributeName = "userPrincipalName";
var = "admin"
您需要像這樣更改過濾器
string filter="(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(attributeName =searchString))";
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com")
var mySearcher = new DirectorySearcher(ent);
mySearcher.Filter = filter;
var userResult = mySearcher.FindOne();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.