码迷,mamicode.com
首页 > 其他好文 > 详细

扩展AuthorizeAttribute

时间:2016-11-10 18:42:48      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:nts   访问权限   ide   pre   ted   sha   htm   ror   csharp   

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 = new ViewDataDictionary() { { "ErrorMessage", Constants.IsAccessDenied } } }; } }
//没有权限时候跳转到的error页面。

 

扩展AuthorizeAttribute

标签:nts   访问权限   ide   pre   ted   sha   htm   ror   csharp   

原文地址:http://www.cnblogs.com/lemonP/p/6051703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!