2021-10-14 20:29:07 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Yi.Framework.Common.Const;
|
2022-04-07 17:57:08 +08:00
|
|
|
|
using Yi.Framework.Common.Helper;
|
|
|
|
|
|
using Yi.Framework.Common.IOCOptions;
|
2022-04-08 23:44:25 +08:00
|
|
|
|
using Yi.Framework.Core;
|
2021-10-14 20:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.WebCore.MiddlewareExtend
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通用跨域扩展
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class JwtExtension
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IServiceCollection AddJwtService(this IServiceCollection services)
|
|
|
|
|
|
{
|
2022-04-07 17:57:08 +08:00
|
|
|
|
services.Configure<JWTTokenOptions>(Appsettings.appConfiguration("JwtAuthorize"));
|
2022-04-08 23:44:25 +08:00
|
|
|
|
services.AddTransient<JwtInvoker>();
|
2022-04-07 17:57:08 +08:00
|
|
|
|
var jwtOptions = Appsettings.app<JWTTokenOptions>("JwtAuthorize");
|
2021-10-14 20:29:07 +08:00
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
2022-04-07 17:57:08 +08:00
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
|
{
|
|
|
|
|
|
ValidateIssuer = true,//是否验证Issuer
|
|
|
|
|
|
ValidateAudience = true,//是否验证Audience
|
|
|
|
|
|
ValidateLifetime = true,//是否验证失效时间
|
2021-10-14 20:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 17:57:08 +08:00
|
|
|
|
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
|
|
|
|
|
ValidAudience = jwtOptions.Audience,//Audience
|
|
|
|
|
|
ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致
|
|
|
|
|
|
IssuerSigningKey = new RsaSecurityKey(RSAFileHelper.GetPublicKey())
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
2021-10-14 20:29:07 +08:00
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|