• 首页

  • 归档
M i c h a e l 的 博 客
M i c h a e l 的 博 客

Michael

获取中...

12
19
.net core

.net core 自定义jwt验证

发表于 2020-12-19 • .net core • 被 15 人看爆

自定义jwt验证

强制注销token等
https://github.com/sevenTiny/Demo.Jwt

using Demo.Jwt.AuthManagement;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.Jwt
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //添加策略鉴权模式
            services.AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateLifetime = true,//是否验证失效时间
                    ClockSkew = TimeSpan.FromSeconds(30),

                    ValidateAudience = true,//是否验证Audience
                    //ValidAudience = Const.GetValidudience(),//Audience
                    //这里采用动态验证的方式,在重新登陆时,刷新token,旧token就强制失效了
                    AudienceValidator = (m, n, z) =>
                    {
                        return m != null && m.FirstOrDefault().Equals(Const.ValidAudience);
                    },
                    ValidateIssuer = true,//是否验证Issuer
                    ValidIssuer = Const.Domain,//Issuer,这两项和前面签发jwt的设置一致

                    ValidateIssuerSigningKey = true,//是否验证SecurityKey
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Const.SecurityKey))//拿到SecurityKey
                };
            });
        }
    }
}
分享到:
docker简介及安装
.net core设置AD登陆验证
  • 文章目录
  • 站点概览
Michael

帅哥Michael

Crowded accidentaly across not anymore

Email RSS
看爆 Top5
  • async + await + promise 301次看爆
  • .net core AWS S3文件存储桶操作 272次看爆
  • ubuntu docker 安装 265次看爆
  • .net core部署环境 237次看爆
  • 常用sql server语句 221次看爆

Copyright © 2021 Michael · 鄂ICP备19024529号

Proudly published with Halo · Theme by fyang · 站点地图