添加JWT认证失败时的自定义响应处理

This commit is contained in:
罗澜大帅哥 2025-07-16 21:58:02 +08:00
parent 5d9da6360a
commit 0589c29e5d

View File

@ -83,6 +83,17 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
ValidateIssuerSigningKey = true,
IssuerSigningKey = secKey
};
x.Events = new JwtBearerEvents
{
OnChallenge = context =>
{
context.HandleResponse();
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
return context.Response.WriteAsJsonAsync(new ReturnTemplate(401,"你提供了一个错误的Token所以我们无法验证你的身份唔......",null));
}
};
})
.AddCookie("Identity.External").AddCookie("Identity.Application");