Я пытаюсь использовать собственную авторизацию, создав базовый контроллер и переопределив метод 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;
, но это не помогает.