2014-02-05 50 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。

相關問題