4

在 MVC 4 项目中使用 SimpleMembershipProvider 时,当没有适当角色的用户运行操作时,他会被重定向到 Account/Login。如何将用户重定向到我自己的“没有足够的权限查看页面”操作?

4

1 回答 1

6

您可以覆盖执行此操作的方法

public class CustomAuthorizeAttribute : AuthorizeAttribute {
    public override void OnAuthorization(AuthorizationContext filterContext) {
        base.OnAuthorization(filterContext);

        if (filterContext == null) {
            throw new ArgumentNullException("filterContext");
        }

        if ({your code detecting no user is logged}) {
            filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" + filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl));
            return;
        }

        if ({your code detecting that the user has no access}) {
            var ViewData = new ViewDataDictionary();
            ViewData.Add("Title", "No access");
            ViewData.Add("Description", "blah blah blah blah blah blah blah ");
            filterContext.Result = new ViewResult { ViewName = "~/Views/Shared/NoAccess.cshtml", ViewData = ViewData };
        }

    }
于 2013-07-16T15:39:33.090 回答