如何通过LDAP域名,以获取用户名和SID用户

编程入门 行业动态 更新时间:2024-10-27 22:26:42
本文介绍了如何通过LDAP域名,以获取用户名和SID用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想获得的用户信息,特定的领域,这将是该程序的输入。关于域名的基础它应该返回的用户名/或NT Id和用户的SID的列表。我是新的LDAP编程任何一个可以帮助我得到这个列表。

I am trying to get the user information for a specific domain which will be the input of the program. On the basis of the domain name it should return the list of the users name/ or NT Id and SID of the user. I am new for the ldap programming can any one help me for get this list.

推荐答案

如果你在.NET 3.5和和谈论的Active Directory,那么你应该看看 System.DirectoryServices.AccountManagement (S.DS.AM)命名空间。阅读所有关于它的:

If you're on .NET 3.5 and up and talking about Active Directory, then you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

  • 在.NET Framework管理目录安全主体3.5
  • 上System.DirectoryServices.AccountManagement MSDN文档
  • Managing Directory Security Principals in the .NET Framework 3.5
  • MSDN docs on System.DirectoryServices.AccountManagement

基本上,你可以定义域范围内,并很容易地找到在AD用户和/或组:

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.... var usersSid = user.Sid; // not sure what you mean by "username" - the "DisplayName" ? The "SAMAccountName"?? var username = user.DisplayName; var userSamAccountName = user.SamAccountName; }

新S.DS.AM使得它可以很容易地玩弄用户和组AD!

The new S.DS.AM makes it really easy to play around with users and groups in AD!

更新:如果您通过域的所有用户需要循环 - 试试这个:

Update: if you need to loop through all the users of a domain - try this:

您可以使用 PrincipalSearcher 和查询通过例如主要做你的搜索:

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 UserPrincipal qbeUser = new UserPrincipal(ctx); // create your principal searcher passing in the QBE principal PrincipalSearcher srch = new PrincipalSearcher(qbeUser); // find all matches foreach(var found in srch.FindAll()) { UserPrincipal user = found as UserPrincipal; if(user != null) { // do whatever here var usersSid = user.Sid; // not sure what you mean by "username" - the "DisplayName" ? var username = user.DisplayName; var userSamAccountName = user.SamAccountName; } }

更新#2:如果您不能(或不愿)使用 S.DS.AM 办法 - 这是最简单的,对于Active Directory,迄今为止 - 那么你需要回退到的System.DirectoryServices 类和方法:

Update #2: if you can't (or don't want to) use the S.DS.AM approach - which is the easiest, for Active Directory, by far - then you need to fall back to the System.DirectoryServices classes and methods:

// define the root of your search DirectoryEntry root = new DirectoryEntry("LDAP://dc=YourCompany,dc=com"); // set up DirectorySearcher DirectorySearcher srch = new DirectorySearcher(root); srch.Filter = "(objectCategory=Person)"; srch.SearchScope = SearchScope.Subtree; // define properties to load srch.PropertiesToLoad.Add("objectSid"); srch.PropertiesToLoad.Add("displayName"); // search the directory foreach(SearchResult result in srch.FindAll()) { // grab the data - if present if(result.Properties["objectSid"] != null && result.Properties["objectSid"].Count > 1) { var sid = result.Properties["objectSid"][0]; } if(result.Properties["displayName"] != null && result.Properties["displayName"].Count > 0) { var userName = result.Properties["displayName"][0].ToString(); } }

更多推荐

如何通过LDAP域名,以获取用户名和SID用户

本文发布于:2023-11-09 17:09:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户名   域名   用户   LDAP   SID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!