C# 检查用户是否为组成员
我有一个代码来检查用户是否是组的成员。 我在登录时使用这个 请注意,我有一个域用户和本地用户,例如C# 检查用户是否为组成员,c#,asp.net,windows,login,active-directory-group,C#,Asp.net,Windows,Login,Active Directory Group,我有一个代码来检查用户是否是组的成员。 我在登录时使用这个 请注意,我有一个域用户和本地用户,例如testdomain\administrator和administrator 这是我使用的代码: using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./" + userGroupName + ",group")) { foreach (object member in (IEnumerable)groupEntry.
testdomain\administrator
和administrator
这是我使用的代码:
using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./" + userGroupName + ",group"))
{
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
string completeName = memberEntry.Name;
DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
if (domainValue != null)
{
completeName = domainValue.Name + "\\" + memberEntry.Name;
}
Global.logger.Info("completeName from " + userGroupName + " = " + completeName);
if (userName.Equals(completeName, StringComparison.InvariantCultureIgnoreCase))
{
Global.logger.Debug("IsUserPartOfWindowsGroup returned True with username =" + userName + " , UserGroupName = " + userGroupName);
return true;
}
}
}
Global.logger.Debug("IsUserPartOfWindowsGroup returned false for username =" + userName + " , UserGroupName = " + userGroupName);
return false;
}
此代码有效,但是
DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
在我看来,在分析器中花费了很多时间。是否有更好/更快的方法来处理此问题
public static DirectoryEntry FindDomain(DirectoryEntry memberEntry)
{
if (memberEntry.Parent != null)
{
if (memberEntry.Parent.SchemaClassName.Equals("domain", StringComparison.InvariantCultureIgnoreCase))
{
return memberEntry.Parent;
}
}
return null;
}
另一种方式:
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, Password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
SearchResult result = mySearcher.FindOne();
Global.logger.Info("result == " + result.Path);
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(adminGroupName))
{
Global.logger.Info(compUsrNameForEncryption + "exists in " + adminGroupName);
}
}
这与我使用的非常接近:
public bool IsUserInGroup(string userName, string groupName)
{
using (var context = new PrincipalContext(ContextType.Machine))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { SamAccountName = userName }))
{
using (var user = searcher.FindOne() as UserPrincipal)
{
return user != null && user.IsMemberOf(context, IdentityType.SamAccountName, groupName);
}
}
}
}
不过,我还没有对本地用户进行测试。逻辑在我的域中工作,我刚刚更改了PrincipalContext(ContextType.Machine)
,所以它现在应该查看本地用户
不要忘记为
System.DirectoryServices.AccountManagement添加一个reference和using语句这与我使用的非常接近:
public bool IsUserInGroup(string userName, string groupName)
{
using (var context = new PrincipalContext(ContextType.Machine))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { SamAccountName = userName }))
{
using (var user = searcher.FindOne() as UserPrincipal)
{
return user != null && user.IsMemberOf(context, IdentityType.SamAccountName, groupName);
}
}
}
}
不过,我还没有对本地用户进行测试。逻辑在我的域中工作,我刚刚更改了PrincipalContext(ContextType.Machine)
,所以它现在应该查看本地用户
别忘了为System.DirectoryServices.AccountManagement添加一个引用和using语句
也许我遗漏了什么,但您不能这样做:
if(Page.User.IsInRole("GROUP NAME"))
{
// user is in group. do your thing
}
else
{
// user isn't in group
}
当我在ASP.NET上执行Active Directory身份验证时,它适用于我
编辑:下面是一个使用页面的描述。User.IsInRole()。但是必须使用Windows身份验证,如果不使用Windows身份验证,它将无法正常工作
EDIT2:因为没有使用Windows身份验证,所以我会这样做:
DirectoryEntry de = new DirectoryEntry(LDAP Address,user,password);
DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = string.Format("(SAMAccountName={0})", user);
SearchResult result = searcher.FindOne();
bool isInGroup = false;
if (result != null)
{
DirectoryEntry person = result.GetDirectoryEntry();
PropertyValueCollection groups = person.Properties["memberOf"];
foreach (string g in groups)
{
if(g.Equals(groupName))
{
isInGroup = true;
break;
}
}
}
return isInGroup;
也许我错过了什么,但你就不能这样做:
if(Page.User.IsInRole("GROUP NAME"))
{
// user is in group. do your thing
}
else
{
// user isn't in group
}
当我在ASP.NET上执行Active Directory身份验证时,它适用于我
编辑:下面是一个使用页面的描述。User.IsInRole()。但是必须使用Windows身份验证,如果不使用Windows身份验证,它将无法正常工作
EDIT2:因为没有使用Windows身份验证,所以我会这样做:
DirectoryEntry de = new DirectoryEntry(LDAP Address,user,password);
DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = string.Format("(SAMAccountName={0})", user);
SearchResult result = searcher.FindOne();
bool isInGroup = false;
if (result != null)
{
DirectoryEntry person = result.GetDirectoryEntry();
PropertyValueCollection groups = person.Properties["memberOf"];
foreach (string g in groups)
{
if(g.Equals(groupName))
{
isInGroup = true;
break;
}
}
}
return isInGroup;
如果我们知道什么是GUIUtility.FindDomain
,我们可能会提供更好的帮助。另外,新的(ish)类使这类事情变得更简单。该问题中的代码可能也会帮助您解决问题。。我添加了finddomain函数。另外,如果用户不是域的一部分,该怎么办?您确定FindDomain
是消耗您的周期的原因吗?它实际上没有任何作用。它不是域的一部分,这意味着它们只是本地用户?如果我们知道什么是GUIUtility.FindDomain
,我们可能会提供更好的帮助。另外,新的(ish)类使这类事情变得更简单。该问题中的代码可能也会帮助您解决问题。。我添加了finddomain函数。另外,如果用户不是域的一部分,该怎么办?您确定FindDomain
是消耗您的周期的原因吗?它实际上没有任何作用。不是域的一部分,这意味着它们只是本地用户?你可能是对的,但我不是ASP.NET开发人员。OP确实表示,他们正在寻找一种可与本地用户/组协同工作的解决方案,但如果您的解决方案能够做到这一点,那么您的答案可能是最好的。@M.Babcock只要OP使用Windows身份验证,那么这将行得通。如果没有,那么你的答案是我不使用windows身份验证。。这是在类中定义的。。所以页面不起作用。。thanks@user175084我已经编辑了我的回复,以反映新信息。我已经尝试过此代码。。如果我提供了一个域用户,这就行了。。但是对于本地用户来说,它将如何工作呢。。原因是我无法找出LDAp路径??你可能是对的,但我不是ASP.NET开发人员。OP确实表示,他们正在寻找一种可与本地用户/组协同工作的解决方案,但如果您的解决方案能够做到这一点,那么您的答案可能是最好的。@M.Babcock只要OP使用Windows身份验证,那么这将行得通。如果没有,那么你的答案是我不使用windows身份验证。。这是在类中定义的。。所以页面不起作用。。thanks@user175084我已经编辑了我的回复,以反映新信息。我已经尝试过此代码。。如果我提供了一个域用户,这就行了。。但是对于本地用户来说,它将如何工作呢。。原因是我无法找出LDAp路径??嘿,我在以下位置得到一个错误NullRefresenceException:return user.IsMemberOf(context,IdentityType.SamAccountName,groupName);我还删除了所有的健全性检查,因此如果用户名
不是有效用户,您将得到该检查。我的原始代码缺少一些使用
s的,因此我添加了它们并进行了必要的健全性检查。我尝试了此操作,但它表明该用户不是该组的一部分。。基本上用户值是空的。。。我将用户名设置为domain\username..您不需要使用domain\part。用户名应该足够(这就是我使用的)。嘿,我在以下位置得到一个错误NullRefresenceException:return user.IsMemberOf(context,IdentityType.SamAccountName,groupName);我还删除了所有的健全性检查,因此如果用户名
不是有效用户,您将得到该检查。我的原始代码缺少一些使用
s的,因此我添加了它们并进行了必要的健全性检查。我尝试了此操作,但它表明该用户不是该组的一部分。。基本上用户值是空的。。。我将用户名设置为domain\username..您不需要使用domain\part。用户名应该足够(这就是我使用的)。