[英]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.