在我的 MVC 4 应用程序SimpleMembershipProvider
中。我正在用属性[Authorize(Roles = "Admin")]
装饰我的控制器。
当Roles为false时,我将用户重定向到登录页面。如何更改它以重定向到索引页面?
答案 0 :(得分:1)
您可以通过将AuthorizeAttribute
替换为
[CustomAuthAttribute(Roles = "Admin")]
代码:
using System;
using System.Web.Http;
using System.Net.Http;
public class CustomAuthAttribute : AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
/* if authorization condition fails */
if(!condition)
{
HandleUnauthorizedRequest(actionContext);
}
}
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "/*ActionName*/" },
{ "controller", "/*ControllerName*/" }
});
}
}