2014-02-05 51 views
0

我试图读出用户,这些用户有权读取文档。我已经可以连接到目录并读出身份参考,但是现在我想在Active Directory中查看ID并从此ID中读出姓名。如何在Active Directory中查找IdentityReference?

DirectorySecurity ds = Directory.GetAccessControl(path); 
        AuthorizationRuleCollection arc = ds.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); 
        foreach (FileSystemAccessRule fsar in arc) 
        { 
          StringBuilder sb = new StringBuilder(); 
          sb.AppendLine("Identity : " + fsar.IdentityReference.Value); 


          sb.AppendLine("FileSystemRights : " + fsar.FileSystemRights); 
          + fsar.PropagationFlags); 


          Console.WriteLine(sb.ToString()); 

我已经可以连接到AD服务器,现在我想用DirectorySearcher搜索IdentityReference。

System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(@"LDAP://mydomain.local/"); 
           entry.Username = username; 
           entry.Password = password; 

          System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry); 

mySearcher.Filter = "(......)"; //searching for IdentityReference 

我该怎么做?

btw:我是C#初学者,对每个答案都非常感谢。

回答

0

正如您所见,here,标识引用是SID(S-1-2-3434-1234243 ...)或NT帐户名称(DOMAIN \ john.doe)。这些可以用Translate方法翻译成彼此,所以你可以使用任何一个。决定你喜欢哪一个并进行翻译。如果引用已经是这种格式,那么无关紧要,翻译总是比较容易的,你可以确定它是任何你喜欢的。

为了找到基于这些属性的用户,我建议使用PrincipalContext.FindByIdentity方法。它支持SID和登录名查找等,并且比LDAP过滤器容易得多。

但是,当然,如果您愿意,您可以编写LDAP过滤器。我不确定登录名,因为它不是以该格式直接存储在AD中,但是如果您为objectSid属性(如(objectSid=S-1-2-3434...))编写查询,那么您肯定可以搜索SID。

相关问题