我有以下类来实现自定义授权系统: -
[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");
,如“您无权执行此操作..”。
感谢
答案 0 :(得分:1)
您可以返回ViewResult
:
var viewResult = new ViewResult();
viewResult.ViewName = "~/Views/Errors/Unauthorized.cshtml";
filterContext.Result = viewResult;
当然,您可以在此视图结果实例上设置其他属性,例如布局和视图需要的可能视图模型。