zoukankan      html  css  js  c++  java
  • 自定义AuthorizeFilter

    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Authorization.Infrastructure;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Authorization;
    using Microsoft.AspNetCore.Mvc.Filters;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using WeTax.CommonLibrary.Models.Enum;
    
    namespace MerchantsApi.Filters
    {
        public class MerchantAuthorizeAttribute : AuthorizeFilter
        {
            private static AuthorizationPolicy _policy_ = new AuthorizationPolicy(new[] { new DenyAnonymousAuthorizationRequirement() }, new string[] { });
    
            public MerchantAuthorizeAttribute() : base(_policy_)
            { }
    
            public override async Task OnAuthorizationAsync(AuthorizationFilterContext context)
            {
                if (string.IsNullOrEmpty(context.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "userid")?.Value))
                {
                    context.Result = new JsonResult(new WeTax.CommonLibrary.Models.WebApiResult(ApiResultCode.Fail, "请登录"));
                }
                await base.OnAuthorizationAsync(context);
            }
        }
    }
                services.AddMvc(options =>
                {
                    options.Filters.Add<MerchantAuthorizeAttribute>();
                    //options.Filters.Add<AuthExceptionFilter>();
                }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  • 相关阅读:
    scrapy 第一个案例(爬取腾讯招聘职位信息)
    scrapy 代码调试用 shell
    scrapy 命令行创建 启动 跟踪
    scrapy 手动编写模板
    scrapy安装
    爬虫之xpath用法
    爬虫之 beautifusoup4
    抓包工具fiddler
    12306登录爬虫 session版本
    Drainage Ditches(dinic)
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10984491.html
Copyright © 2011-2022 走看看