2008-09-05 20 views
3

我想知道是否有人知道如何通過C#以編程方式獲得遠程服務器上本地組的成員資格。這是否需要管理員權限?如果有,是否有任何方法可以確認當前登錄用戶的會員(或不會)這些組?通過C#確定本地組的成員

+0

可能重複http://stackoverflow.com/questions/21514/enumerate-windows-user-group-members-on-remote-system-使用-c-sharp) – 2012-04-02 08:41:24

回答

0

也許這是可以通過WMI完成的事情?

4

Howto: (Almost) Everything In Active Directory via C#非常有幫助,還包括如何迭代組中的AD成員的說明。

public ArrayList Groups(string userDn, bool recursive) 
{ 
    ArrayList groupMemberships = new ArrayList(); 
    return AttributeValuesMultiString("memberOf", userDn, 
     groupMemberships, recursive); 
} 

你也需要這樣的功能:

public ArrayList AttributeValuesMultiString(string attributeName, 
    string objectDn, ArrayList valuesCollection, bool recursive) 
{ 
    DirectoryEntry ent = new DirectoryEntry(objectDn); 
    PropertyValueCollection ValueCollection = ent.Properties[attributeName]; 
    IEnumerator en = ValueCollection.GetEnumerator(); 

    while (en.MoveNext()) 
    { 
     if (en.Current != null) 
     { 
      if (!valuesCollection.Contains(en.Current.ToString())) 
      { 
       valuesCollection.Add(en.Current.ToString()); 
       if (recursive) 
       { 
        AttributeValuesMultiString(attributeName, "LDAP://" + 
        en.Current.ToString(), valuesCollection, true); 
       } 
      } 
     } 
    } 
    ent.Close(); 
    ent.Dispose(); 
    return valuesCollection; 
} 

如果你現在想用這個AD-方法,你可以在本文中使用的信息,但它使用非託管代碼:

http://www.codeproject.com/KB/cs/groupandmembers.aspx

,他們作出的示例應用程序:

alt text

+1

Localgroups是服務器本地的;它們不存儲在AD中。儘管他們可能包括AD用戶和/或組作爲成員。 – quux 2008-09-12 06:40:02

+0

此答案顯示了枚舉AD用戶但不包含本地計算機組的一種方法。 – hal9000 2013-03-15 01:03:19

2

看起來在.net 3.5中有一個名爲System.DirectoryServices.AccountManagement的新程序集,它比System.DirectoryServices提供了一個更清晰的實現。 Dominick Baier blogs about幾個簡單的操作,包括檢查組的成員資格: -

public static bool IsUserInGroup(string username, string groupname, ContextType type) 
{ 
    PrincipalContext context = new PrincipalContext(type); 

    UserPrincipal user = UserPrincipal.FindByIdentity(
     context, 
     IdentityType.SamAccountName, 
     username); 
    GroupPrincipal group = GroupPrincipal.FindByIdentity(
     context, groupname); 

    return user.IsMemberOf(group); 
} 

我想我會用這種方法,感謝雖然不過建議! :-)

0

我問了一個類似的問題,最後寫了一個answer,它使用WMI來枚舉組成員。我在system.directoryservices.accountmanagement中遇到了身份驗證問題。 YMMV,當然。

0

我很好奇,如果System.DirectoryServices.AccountManagement完全管理。我已經使用了System.DirectoryServices.ActiveDirectory,它是COM Interop的一個包裝,這導致了許多令人頭痛的問題......

0

這可能會有所幫助。我不得不開發一個應用程序,我們要根據活動目錄進行身份驗證,並檢查用戶所在的組字符串。

由於幾個原因,我們不想使用Windows身份驗證,而是讓我們的自己的基於表單的認證我開發了下面的例程來首先驗證用戶,然後檢查用戶所屬的所有組。也許它可能有幫助。該例程使用LogonUser進行身份驗證,然後獲取該用戶的數字guid-like組標識(SID)列表,並將每個標識轉換爲可讀的形式。

希望這有助於我必須從各種不同的谷歌搜索合成這種方法。

private int validateUserActiveDirectory() 
{ 
    IntPtr token = IntPtr.Zero; 
    int DBgroupLevel = 0; 

    // make sure you're yourself -- recommended at msdn http://support.microsoft.com/kb/248187 
    RevertToSelf(); 

    if (LogonUser(txtUserName.Value, propDomain, txtUserPass.Text, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) != 0) { 
     // ImpersonateLoggedOnUser not required for us -- we are not doing impersonated stuff, but leave it here for completeness. 
     //ImpersonateLoggedOnUser(token); 
     // do impersonated stuff 
     // end impersonated stuff 

     // ensure that we are the original user 
     CloseHandle(token); 
     RevertToSelf(); 

     System.Security.Principal.IdentityReferenceCollection groups = Context.Request.LogonUserIdentity.Groups; 
     IdentityReference translatedGroup = default(IdentityReference); 

     foreach (IdentityReference g in groups) { 
      translatedGroup = g.Translate(typeof(NTAccount)); 
      if (translatedGroup.Value.ToLower().Contains("desired group")) { 
       inDBGroup = true; 
       return 1; 
      } 
     } 
    } 
    else { 
     return 0; 
    } 
} 
[遠程系統上枚舉Windows用戶組成員使用C#(的