Update Authentication_JWTSetup.cs

This commit is contained in:
anjoy8 2023-11-18 08:53:47 +08:00
parent b3e7fbca54
commit c93c3eb44b

View File

@ -5,11 +5,8 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Blog.Core.Extensions namespace Blog.Core.Extensions
{ {
@ -74,7 +71,7 @@ namespace Blog.Core.Extensions
}, },
OnChallenge = context => OnChallenge = context =>
{ {
context.Response.Headers.Add("Token-Error", context.ErrorDescription); context.Response.Headers["Token-Error"] = context.ErrorDescription;
return Task.CompletedTask; return Task.CompletedTask;
}, },
OnAuthenticationFailed = context => OnAuthenticationFailed = context =>
@ -88,12 +85,12 @@ namespace Blog.Core.Extensions
if (jwtToken.Issuer != Issuer) if (jwtToken.Issuer != Issuer)
{ {
context.Response.Headers.Add("Token-Error-Iss", "issuer is wrong!"); context.Response.Headers["Token-Error-Iss"] = "issuer is wrong!";
} }
if (jwtToken.Audiences.FirstOrDefault() != Audience) if (jwtToken.Audiences.FirstOrDefault() != Audience)
{ {
context.Response.Headers.Add("Token-Error-Aud", "Audience is wrong!"); context.Response.Headers["Token-Error-Aud"] = "Audience is wrong!";
} }
} }
@ -101,7 +98,7 @@ namespace Blog.Core.Extensions
// 如果过期,则把<是否过期>添加到,返回头信息中 // 如果过期,则把<是否过期>添加到,返回头信息中
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException)) if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
{ {
context.Response.Headers.Add("Token-Expired", "true"); context.Response.Headers["Token-Expired"] = "true";
} }
return Task.CompletedTask; return Task.CompletedTask;
} }