【问题标题】:Redirect user when not authorised to page in MVC3未授权在 MVC3 中分页时重定向用户
【发布时间】:2012-05-16 00:24:51
【问题描述】:

我有一个 mvc 3 项目,我使用 AD 来授权用户。 我有一个页面,只有具有“管理员”角色的用户才能访问。

我已使登录工作正常并授权用户,因此只有管理员可以访问网站的管理部分。 我遇到的问题是当用户不是管理员时,我似乎无法显示好的错误消息。

这是我的 actionFilter 属性

public class AdminOnlyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        bool isAuthorised = false;

        IPrincipal user = filterContext.HttpContext.User;
        if (user.Identity.IsAuthenticated)
        {
            if (user.IsInRole("Admin"))
            {
                isAuthorised = true;
            }
        }

        if (!isAuthorised)
        {
            //error message here
        }
    }

这是我的管理员控制器

    [AdminOnly]
        public ActionResult Index()
        {
        //admin stuff
        }

任何帮助表示赞赏,在此先感谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 error-handling active-directory


    【解决方案1】:

    如果isAuthorised 为假,您需要将他们带到可以显示“您无权访问该页面等”的页面。要进行重定向,您必须执行以下操作(在我的示例中,我将它们重定向到 Account/AccessDenied 这将返回一个视图,其中包含一条消息“您没有访问权限等...”:

    if (!isAuthorised)
    {
        filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary {{"action", "AccessDenied"}, 
                {"controller", "Account"}});
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2020-03-10
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多