活动目录访问

发布于 2024-12-22 04:22:29 字数 846 浏览 0 评论 0原文

我需要从活动目录访问信息。我正在使用代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-12-29 04:22:29

如果您使用的是 .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, "SomeUserName");

if(user != null)
{
   // do something here....     
   string emailAddress = user.EmailAddress;
}

UserPrincipal 类包含大量您可以读出的属性(并且也设置新值)。

新的 S.DS.AM 使 AD 中的用户和组的使用变得非常容易!

当然 - 您还可以搜索用户!

您可以使用 PrincipalSearcher 和“按示例查询”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the SAMAccountName of "ad_id"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "ad_id";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    UserPrincipal foundUser = found as UserPrincipal;

    if(foundUser != null)
    {
       // do something here....     
       string emailAddress = foundUser.EmailAddress;
    }
}

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:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
   string emailAddress = user.EmailAddress;
}

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:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the SAMAccountName of "ad_id"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "ad_id";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    UserPrincipal foundUser = found as UserPrincipal;

    if(foundUser != null)
    {
       // do something here....     
       string emailAddress = foundUser.EmailAddress;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文