我正在尝试验证用户是否在"TestGroup“组中。用户是"TestGroup“组的一部分,即使我得到的是retval =TestGroup @line(retVal = user.IsMemberOf(groupPrincipal);),在事件查看器中,它显示消息为”用户名或密码不正确“。
你能帮我解决这个问题吗?
string userName = this.Request.ServerVariables["AUTH_USER"];
if (ValidateUser(userName) == false)
Response.Redirect("Error.aspx?errormsg=" + userName + " does not have permission to view this page");
public static bool ValidateUser(String userName)
{
bool useGroupAuthorization = true;
if (useGroupAuthorization)
return GroupLookup(userName, "TestGroup");
}
private static bool GroupLookup(string userName, string groupName)
{
System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
appLog.Source = "Test App";
bool retVal = false;
PrincipalContext pc = null;
UserPrincipal user = null;
GroupPrincipal groupPrincipal = null;
try
{
string strdomain = "TestDomain";
pc = new PrincipalContext(ContextType.Domain,strdomain);
user = UserPrincipal.FindByIdentity(pc, userName);
groupPrincipal = GroupPrincipal.FindByIdentity(pc, groupName);
retVal = user.IsMemberOf(groupPrincipal);
}
catch (NoMatchingPrincipalException nmpx)
{
appLog.WriteEntry(nmpx.Message);
}
catch (PrincipalOperationException pox)
{
appLog.WriteEntry(pox.Message);
}
catch (Exception ex)
{
if (user == null)
{
appLog.WriteEntry(ex.Message);
}
else
{
appLog.WriteEntry(ex.Message);
}
}
return retVal;
}
// when i tried with below code i am getting userPrincipal is null
// bool retVal = false; string strdomain = "TestDomain";
// PrincipalContext principalCtx = new PrincipalContext(ContextType.Domain, strdomain);
// UserPrincipal queryByExampleUser = new UserPrincipal ( principalCtx );
// queryByExampleUser.SamAccountName = userName;
// PrincipalSearcher principalSearcher = new PrincipalSearcher ( );
// principalSearcher.QueryFilter = queryByExampleUser;
// UserPrincipal userPrincipal = principalSearcher.FindOne ( ) as UserPrincipal;
// retVal = IsUserInGroup("TestGroup", userPrincipal);
// return retVal;
// }
//static bool IsUserInGroup(string groupName, UserPrincipal user)
//{
// PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
// GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
// if (user.IsMemberOf(groupPrincipal))
// {
// return true;
// }
// return false;
//}发布于 2019-06-29 14:27:31
"gpKnownAccountToCheck.Members“不是递归的。
var result = groupPrincipal
.GetMembers(true)
.Where(x => x.Sid == userPrincipal.Sid)
.Count() > 0;发布于 2018-04-24 00:08:23
UserPrincipal.IsMemberOf(GroupPrincipal)似乎适用于某些小组,而不适用于其他小组。在我的域中,它只适用于domain\Developers (自定义组),而不适用于domain\Domain Users。去想一想。我停止了调试器中的代码,并检查了域用户组的成员列表,发现我的用户在其中,但IsMemberOf仍然返回false。然而,我发现如果我循环遍历GroupPrincipal.Members中的UserPrincipal对象集合,我可以通过将集合中的UserPrincipal与我要搜索的for进行比较来进行检查。很糟糕,但我能找到的唯一可靠的解决方案。
示例代码:
string sAccountToCheckSID = upAccountToCheck.Sid.Value;
foreach(UserPrincipal up in gpKnownAccountToCheck.Members)
{
string sKnownSIDInGroup = up.Sid.Value;
if(sKnownSIDInGroup.Equals(sAccountToCheckSID))
{
userMatchingUserObject = userKnownUser;
return true;
}
}所以我不知道为什么。但这就是我的工作。
https://stackoverflow.com/questions/25491042
复制相似问题