我想知道是否有人知道如何通過C#以編程方式獲得遠程服務器上本地組的成員資格。這是否需要管理員權限?如果有,是否有任何方法可以確認當前登錄用戶的會員(或不會)這些組?通過C#確定本地組的成員
回答
也許這是可以通過WMI完成的事情?
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
,他們作出的示例應用程序:
看起來在.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);
}
我想我會用這種方法,感謝雖然不過建議! :-)
我問了一個類似的問題,最後寫了一個answer,它使用WMI來枚舉組成員。我在system.directoryservices.accountmanagement中遇到了身份驗證問題。 YMMV,當然。
我很好奇,如果System.DirectoryServices.AccountManagement完全管理。我已經使用了System.DirectoryServices.ActiveDirectory,它是COM Interop的一個包裝,這導致了許多令人頭痛的問題......
這可能會有所幫助。我不得不開發一個應用程序,我們要根據活動目錄進行身份驗證,並檢查用戶所在的組字符串。
由於幾個原因,我們不想使用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#(的
- 1. 通過SID以多種語言向本地組添加成員
- 2. 列出本地組成員
- 3. 獲取本地組的成員
- 4. 通過API列出facebook組的成員
- 5. 本地C++數組通過C++/CLI C#數組
- 6. C++ - 通過成員函數更改一個類的成員值
- 7. 通過地址訪問結構成員
- 8. 通過反射確定類成員的類型
- 9. C++類與數組成員的大小通過CTOR
- 10. C:通過指針設置結構的數組成員
- 11. C++,成員地址
- 12. 確定Vista中本地管理員組的名稱
- 13. 確定是否用戶是組成員
- 14. C++類成員充當本地變量
- 15. C#的形式通過本地C++
- 16. 通過本地靜態實例訪問實例成員
- 17. 通過給定用戶名和域名的腳本查找組成員身份
- 18. 自動搜索本地「管理員」組的成員
- 19. 通過PowerShell將通訊組成員導入安全組
- 20. 通過外部訪問靜態成員並通過繼承C++
- 21. powershell:通過過濾列出一個組中的成員
- 22. 通過數組的最低成本
- 23. 確定現有的本地窗口通過.NET
- 24. 通過訪問成員
- 25. 通過參數確定的C++傳遞動態數組
- 26. 如何通過本地網絡正確執行C#文件?
- 27. C++類的成員函數,並明確通過DLL鏈接到他們
- 28. 用戶has_many成員資格和has_many組織通過成員資格。如何指定默認會員資格?
- 29. 什麼是通過authorize.net升級成員的正確方法
- 30. 如何正確記錄通過Object.defineProperties添加的實例成員?
可能重複http://stackoverflow.com/questions/21514/enumerate-windows-user-group-members-on-remote-system-使用-c-sharp) – 2012-04-02 08:41:24