我想知道是否有人知道如何通过 C# 以编程方式获取远程服务器上本地组的成员资格。这需要管理员权限吗?如果是这样,有什么方法可以确认当前登录用户的这些组的成员身份(或不是)?
6 回答
Howto:(几乎)通过 C# 在 Active Directory 中的所有内容都非常有用,还包括有关如何在组中迭代 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
他们制作的示例应用程序:
It appears there is a new Assembly in .net 3.5 called System.DirectoryServices.AccountManagement which gives a cleaner implementation than System.DirectoryServices. Dominick Baier blogs about a couple of simple operations including checking membership of a group:-
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);
}
I think I will use this approach, thanks for the suggestions though however! :-)
我问了一个类似的问题,最后写了一个使用 WMI 枚举组成员的答案。我在 system.directoryservices.accountmanagement 东西中的身份验证方面遇到了真正的问题。YMMV,当然。
我很好奇 System.DirectoryServices.AccountManagement 是否得到完全管理。我使用过 System.DirectoryServices.ActiveDirectory ,它是 COM 互操作的包装器,它导致了许多令人头疼的问题......
也许这可以通过 WMI 完成?
这可能会有所帮助。我必须开发一个应用程序,我们要在其中对 Active Directory 进行身份验证,并检查用户所在的组字符串。
出于几个原因,我们不想使用 Windows 身份验证,而是使用我们自己的基于表单的身份验证。我开发了下面的例程,首先对用户进行身份验证,然后检查用户所属的所有组。也许它可能会有所帮助。该例程使用 LogonUser 进行身份验证,然后获取该用户的类似 guid 的数字组 id (SID) 列表,并将每个组 id 转换为人类可读的形式。
希望这会有所帮助,我必须从各种不同的谷歌搜索中综合这种方法。
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;
}
}