12345678910111213141516171819202122232425262728293031323334353637383940 |
- using Infrastructure;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.IdentityModel.Tokens;
- using System;
- using System.Threading.Tasks;
- namespace ZR.Infrastructure.WebExtensions
- {
- public static class JwtExtension
- {
- public static void AddJwt(this IServiceCollection services)
- {
- services.AddAuthentication(options =>
- {
- options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- }).AddCookie()
- .AddJwtBearer(o =>
- {
- o.TokenValidationParameters = JwtUtil.ValidParameters();
- o.Events = new JwtBearerEvents
- {
- OnAuthenticationFailed = context =>
- {
- // 如果过期,把过期信息添加到头部
- if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
- {
- Console.WriteLine("jwt过期了");
- context.Response.Headers.Append("Token-Expired", "true");
- }
- return Task.CompletedTask;
- },
- };
- });
- }
- }
- }
|