我已经实现了一个自定义属性,它基本上检查用户是否在某个组内。如果是,它会处理未经授权的请求。我遇到的问题是代码不断循环并查找以查明问题。如果我有
我的代码如下:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (base.AuthorizeCore(httpContext))
{
var context = new PrincipalContext(ContextType.Domain, "DOMAIN");
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, httpContext.User.Identity.Name);
if (userPrincipal != null)
{
PrincipalSearchResult<Principal> groups = userPrincipal.GetGroups();
bool found = groups.Any(item => item.Name == "NAME OF GROUP HERE");
if (found)
{
return false;
}
}
return true;
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{"action", "AccessDenied"},
{"controller", "Error"}
});
}
我在 global.asax 中添加了上述内容并添加到过滤器中。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AuthorizeADAtttribute());
}
希望有人可以帮助我。
谢谢