2010-04-08 94 views
0

我的代碼已經與舊的Active Directory服務器一起工作,現在我已將它指向新的Windows Server 2008 AD系統。現在,羣組名稱將返回SID而不是名稱。我對AD方面知之甚少,不知道是否有辦法讓新的AD服務器像舊的AD服務器一樣工作。如何從.NET Active Directory組中獲取組名稱?

我的代碼基於Codeplex上BlogEngine.NET的Active Directory角色提供程序。

http://blogengineadrp.codeplex.com/sourcecontrol/network/Show?projectName=BlogEngineADRP&changeSetId=5843#138380

我相信這一點,我將需要調整行。

IdentityReferenceCollection irc = ExpandTokenGroups(user).Translate(typeof(NTAccount)); 

此時ExpandTokenGroups正在返回SID值,而Translate已將其更改爲人類可讀組名。我想知道是否應該通過與NTAccount不同的類型進行翻譯。

我能做些什麼來獲得組名?

回答

0

這是我在C#中的解決方案,它不是最優的,但它工作。

public override string[] GetRolesForUser(string username) 
    { 
     // list to store names of roles 
     List<String> roles = new List<string>(); 

     // get the user directory entry 
     DirectoryEntry user = getUser(username); 

     foreach (String prop in user.Properties["memberOf"]) 
     { 
      if (prop.IndexOf("CN=") == 0 && prop.IndexOf(",") != -1) 
      { 
       var groupName = prop.Substring("CN=".Length, prop.IndexOf(",") - "CN=".Length); 
       roles.Add(groupName); 
      } 
     } 

     return roles.ToArray(); 
    } 
0

解決方案有一個很好的解釋:Translating Between Names and SIDs

基本上,您可以調用LsaLookupSids函數調用。

希望這有助於!

+0

看來解決方案只適用於C++,這是不幸的。我希望.NET對Active Directory和LDAP有更好的支持,因爲它很重要。 – Brennan 2010-04-08 21:06:01

相關問題