我正在嘗試獲取將作爲程序輸入的特定域的用戶信息。在域名的基礎上,它應該返回用戶名稱/或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. 如何在NetSqlAZMAN中獲取應用組,SID和用戶名
- 5. 獲取用戶名和域名(域\用戶)在ASP.Net C#
- 6. 獲取用戶名和域名在C++
- 7. 如何通過$ _SESSION獲取用戶名?
- 8. 通過SID獲取本地用戶
- 9. LDAP獲取完整的用戶名C++
- 10. 如何在MODx中通過用戶名獲取用戶?
- 11. SSO獲取用戶名/域
- 12. Facebook的 - 通過API獲取用戶名
- 13. Python:如何獲取登錄用戶的域\用戶名
- 14. 如何獲取用戶域\用戶名和顯示名稱從SharePoint People Picker InternalId?
- 15. 通過用戶名和密碼與Laravel獲取用戶
- 16. 如何獲取在C++中使用我的服務的用戶的SID和用戶名
- 17. Windows域帳戶SID和SID
- 18. 獲取用戶名和ASP.NET通用Hanlder
- 19. 如何獲得AD組的用戶在指定的用戶名和域名
- 20. 通過greasemonkey獲取用戶名
- 21. 獲取用戶名通過.NET
- 22. simplemembership MVC4通過userId獲取用戶名
- 23. Rails 3通過ID獲取用戶名
- 24. OpenStack keystoneclient通過名稱獲取用戶
- 25. 通過獲取用戶名字段
- 26. 如何在沒有域名的情況下獲取用戶名
- 27. 如何手動檢查ldap用戶的用戶名和密碼
- 28. 如何通過在WordPress中搜索用戶的全名來獲取用戶ID
- 29. 通過瀏覽器獲取登錄用戶的用戶名
- 30. 如何獲取用戶名?
當你說* LDAP *,你的意思是*的Active Directory *在Windows上,還是需要對所有可能的LDAP服務器.... – 2012-02-28 05:51:08