2
我想通過創建一個基本控制器並重寫OnAuthorization方法來使用我自己的授權。自定義授權
授權失敗時工作正常,但當我的檢查成功時(但默認授權檢查失敗),我得到了401頁。
protected override void OnAuthorization(AuthorizationContext filterContext)
{
var roleAttribute = typeof(AuthorizeAttribute);
var attributes = filterContext.ActionDescriptor.GetCustomAttributes(roleAttribute, true);
if (attributes.Length == 0)
attributes = GetType().GetCustomAttributes(roleAttribute, true);
if (attributes.Length == 0)
return;
MvcHelper.Authenticate();
foreach (AuthorizeAttribute item in attributes)
{
if (!Thread.CurrentPrincipal.IsInRole(item.Roles))
{
filterContext.Result = new RedirectResult("~/Error/Unauthorized/" + "?MissingRole=" + item.Roles);
return;
}
}
//how do I prevent the default authorization here?
}
我試過filterContext.HttpContext.SkipAuthorization = true;
但它沒有幫助。