首页 > 代码库 > 扩展AuthorizeAttribute
扩展AuthorizeAttribute
MVC中经常会用到关于设置访问权限的问题:
如果我们扩展了AuthorizeAttribute,那么我们只需要在类或方法前加上此attribute,即可实现权限问题。
AttributeTargets 权限适用于类或者方法
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,Inherited=true,AllowMultiple=true)] public sealed class SecurityAuthorizationAttribute :AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) { throw new ArgumentException("httpContext"); } bool result = true; // 权限设置
return result; } //return true 是不会触发 handleUnauthorizedRequest.
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { //base.HandleUnauthorizedRequest(filterContext); filterContext.Result = new ViewResult { ViewName = "~/Views/Shared/Error.cshtml", ViewData = http://www.mamicode.com/new ViewDataDictionary() { {"ErrorMessage", Constants.IsAccessDenied } } }; } }
//没有权限时候跳转到的error页面。
扩展AuthorizeAttribute
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。