gpt4 book ai didi

c# - UserPrincipal.IsMemberOf 返回 false

转载 作者:太空狗 更新时间:2023-10-29 23:45:23 32 4
gpt4 key购买 nike

我正在尝试验证用户是否在“TestGroup”组中。用户是“TestGroup”组的一部分,即使我收到 retval = false @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;
//}

最佳答案

“gpKnownAccountToCheck.Members”不是递归的。

需要使用方法:GetMembers(recursive: true)

 var result = groupPrincipal
.GetMembers(true)
.Where(x => x.Sid == userPrincipal.Sid)
.Count() > 0;

关于c# - UserPrincipal.IsMemberOf 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25491042/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com