【问题标题】:How to get username and SID for user by a domain name in ldap如何通过ldap中的域名获取用户的用户名和SID
【发布时间】:2012-02-28 05:48:29
【问题描述】:

我正在尝试获取将作为程序输入的特定域的用户信息。根据域名,它应该返回用户名列表/或用户的 NT Id 和 SID。我是 ldap 编程的新手,任何人都可以帮助我获取此列表。

【问题讨论】:

  • 当您说 LDAP 时,您是指 Windows 上的 Active Directory,还是需要针对所有可能的 LDAP 服务器的“通用”LDAP 解决方案....

标签: c# active-directory


【解决方案1】:

如果您使用 .NET 3.5 及更高版本并谈论 Active Directory,那么您应该查看 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....     
   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();
   }
}

【讨论】:

  • @Eshwer:阅读我链接的文章!如果您使用的是 Active Directory(您还没有确认这一点......),构建 PrincipalContext 将使用默认的 AD 域(除非另有说明) - 所以您基本上不需要为当前指定任何内容默认 AD 域。
  • @Eshwer:您需要为该程序集添加引用 - 它在 .NET 3.5 和 4.0 中可用 - 但仅在“完整版本”9f .NET 中 - 不是.NET 4 客户端配置文件
  • @Eshwer:这是 最简单 的方法......什么不适合你?为什么你不能使用最简单的方法来达到目标​​?您正在使用Active Directory - 对吧?? (你还没有确认——我已经问过好几次了......)
  • @Eshwer: 检查MSDN documentation - 该类有几个重载的构造函数
  • @NTDLS:这里没有必要明确指定IdentityType.SamAccountName——恰​​恰相反!如果您这样做,那么您将仅搜索 SAM 帐户名称。如果不指定它,您将搜索几个与名称相关的属性,并且找到您的用户的机会更高!
猜你喜欢
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
相关资源
最近更新 更多