且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-30 07:42:23

如果你在.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:

  • 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();
   }
}