活动目录访问
我需要从活动目录访问信息。我正在使用代码,
DirectoryEntry entry = new DirectoryEntry("LDAP://domain", "AD_id", "password");
DirectorySearcher search = new DirectorySearcher(entry);
try
{
search.Filter = "(SAMAccountName=AD_id)";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("email");
SearchResult result = search.FindOne();
if (result != null)
lbl_result.Text = result.Path.ToString();
else
lbl_result.Text = "failier";
}
catch (Exception ex)
{
lbl_result.Text = ex.Message;
}
我成功地以给定的格式获取了有关用户的一些信息
LDAP://domain/CN=username,OU=aaa,OU=bbb,OU=ccc,DC=domain,DC=com
,但这不是我需要的完整信息,例如电子邮件地址不在上面的字符串中。(aaa、bbb 和 ccc 是其他一些信息) 如果我做错了什么,请帮助我。 我对这种编程很陌生。 我会很感激的。
I need to access information from active directory. I am using code
DirectoryEntry entry = new DirectoryEntry("LDAP://domain", "AD_id", "password");
DirectorySearcher search = new DirectorySearcher(entry);
try
{
search.Filter = "(SAMAccountName=AD_id)";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("email");
SearchResult result = search.FindOne();
if (result != null)
lbl_result.Text = result.Path.ToString();
else
lbl_result.Text = "failier";
}
catch (Exception ex)
{
lbl_result.Text = ex.Message;
}
I successfully get a few information about the user in the given format
LDAP://domain/CN=username,OU=aaa,OU=bbb,OU=ccc,DC=domain,DC=com
But this is not complete information that I need, for example email address is not in the above string.(aaa, bbb and ccc are some other information)
Please help me if I am doing some thing wrong.
I am new to this kind of programming.
I will be thankful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关内容:
基本上,您可以定义域上下文并轻松地在 AD 中查找用户和/或组:
UserPrincipal
类包含大量您可以读出的属性(并且也设置新值)。新的 S.DS.AM 使 AD 中的用户和组的使用变得非常容易!
当然 - 您还可以搜索用户!
您可以使用
PrincipalSearcher
和“按示例查询”主体进行搜索:If you're on .NET 3.5 and up, you should check out the
System.DirectoryServices.AccountManagement
(S.DS.AM) namespace. Read all about it here:Basically, you can define a domain context and easily find users and/or groups in AD:
The
UserPrincipal
class contains a great many properties which you can read out (and set new values for, too).The new S.DS.AM makes it really easy to play around with users and groups in AD!
And of course - you can also search for users!
You can use a
PrincipalSearcher
and a "query-by-example" principal to do your searching: