我的目标是检查用户是否是特定活动目录组的成员。
In .net mvc我在我的服务中使用了这段代码
HttpContext.Current.Request.LogonUserIdentity.Groups
.Any(x => x.Translate(typeof(NTAccount)).Value == "some role"
效果很好。
在.net核心mvc 2.1.2 i pass IHttpContextAccessor
进入服务构造函数并尝试使用以下
_httpAccessor.HttpContext.User.Identity.LogonUserIdentity.Groups
但有一个问题,因为Identity
不包含LogonUserIdentity
。我尝试找到任何解决方案,但没有成功,如何获取用户组列表或检查用户是否是特定组的成员?
除了使用内置功能按“角色”检查权限外,如果您想按特定AD组检查权限,还可以使用以下代码:
public static class Security
{
public static bool IsInGroup(this ClaimsPrincipal User, string GroupName)
{
var groups = new List<string>();
var wi = (WindowsIdentity)User.Identity;
if (wi.Groups != null)
{
foreach (var group in wi.Groups)
{
try
{
groups.Add(group.Translate(typeof(NTAccount)).ToString());
}
catch (Exception)
{
// ignored
}
}
return groups.Contains(GroupName);
}
return false;
}
}
并用作:
if (User.IsInGroup("GroupName"))
{
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)