0

我有以下类来实现自定义授权系统:-

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class CheckUserPermissionsAttribute : ActionFilterAttribute
    {
        Repository repository = new Repository();
        public string Model { get; set; }
        public string Action { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string ADusername = filterContext.HttpContext.User.Identity.Name.Substring(filterContext.HttpContext.User.Identity.Name.IndexOf("\\") + 1);
            if (!repository.can(ADusername,Model,Action))            {
                filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");


            }

            base.OnActionExecuting(filterContext);
        }
    }

但目前当使用 Firefox 访问我的 Web 应用程序时,如果 filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");返回,用户将不断收到提示窗口以永久输入用户名和密码。那么有没有办法修改我的操作方法,而不是返回 filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");,而是返回显示消息的视图,例如“您无权执行此操作..”。谢谢

4

1 回答 1

1

你可以返回一个ViewResult

var viewResult = new ViewResult();
viewResult.ViewName = "~/Views/Errors/Unauthorized.cshtml";
filterContext.Result = viewResult;

如果视图需要,您当然可以在此视图结果实例上设置其他属性,例如布局和可能的视图模型。

于 2013-10-29T11:45:14.333 回答