zoukankan      html  css  js  c++  java
  • 扩展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 = new ViewDataDictionary() { { "ErrorMessage", Constants.IsAccessDenied } } }; } }
    //没有权限时候跳转到的error页面。
  • 相关阅读:
    BZOJ 5018 [Snoi2017]英雄联盟
    BZOJ 4945 [Noi2017]游戏
    BZOJ4942 [Noi2017]整数
    BZOJ 2427 [HAOI2010]软件安装
    BZOJ 4870 [Shoi2017]组合数问题
    THINKPHP 全局404
    PHP 万能查询代码
    xml Array 相互转化
    JS 倒计时计算
    PHP 多态
  • 原文地址:https://www.cnblogs.com/lemonP/p/6051703.html
Copyright © 2011-2022 走看看