我正在尝试获取将作为程序输入的特定域的用户信息。在域名的基础上,它应该返回用户名称/或NT Id和SID的列表。我是ldap编程的新手,任何人都可以帮助我获得此列表。如何通过ldap中的域名获取用户的用户名和SID
6
A
回答
15
如果您使用的是.NET 3.5或更高版本,并且正在讨论Active Directory,那么您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。在这里阅读全部内容:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
基本上,你可以定义域范围内,并可以轻松地查找用户和/或组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中的用户和群组玩耍!
更新:如果通过域的所有用户需要循环 - 试试这个:
您可以使用PrincipalSearcher
和“查询通过例如”主要做你的搜索:
// 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
课程和方法:
// 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();
}
}
相关问题
- 1. LDAP通过用户名和域名获取用户DN
- 2. PHP LDAP获取用户SID
- 3. 仅通过帐户名称获取Windows用户帐户SID
- 4. 如何从非管理员用户获取当前登录的用户名,域名和用户sid?
- 5. 如何在NetSqlAZMAN中获取应用组,SID和用户名
- 6. 获取LDAP用户名无效过滤
- 7. 如何从LDAP获取用户名?
- 8. LDAP搜索通过用户名得到用户的名称
- 9. 获取用户名和域名(域\用户)在ASP.Net C#
- 10. 如何通过$ _SESSION获取用户名?
- 11. 获取用户名和域名在C++
- 12. 如何从域名DNS域名获取域名sid?
- 13. 通过SID获取本地用户
- 14. LDAP获取完整的用户名C++
- 15. 如何在MODx中通过用户名获取用户?
- 16. SSO获取用户名/域
- 17. Facebook的 - 通过API获取用户名
- 18. 从只是一个域用户名获得的的SecurityIdentifier(SID)(C#)
- 19. Python:如何获取登录用户的域\用户名
- 20. 可以通过Google +域名API获取域名下的用户列表?
- 21. 如何获取用户域\用户名和显示名称从SharePoint People Picker InternalId?
- 22. 通过用户名和密码与Laravel获取用户
- 23. JSF获取登录用户的用户名ldap
- 24. 如何获取在C++中使用我的服务的用户的SID和用户名
- 25. 如何通过其主用户名获取所有值,其中用户名与用户名相关
- 26. Windows域帐户SID和SID
- 27. 从C++中的SID获取用户名失败?
- 28. 获取用户名和ASP.NET通用Hanlder
- 29. 如何通过Django中的user_id获取用户名?
- 30. 如何获得AD组的用户在指定的用户名和域名
当你说* LDAP *,你的意思是*的Active Directory *在Windows上,还是需要对所有可能的LDAP服务器.... – 2012-02-28 05:51:08