using Gatedge.K3Cloud.Utils;
using Gatedge.K3Cloud.Utils.Option;
using Gatedge.ScanCode.Options;
using Gatedge.ScanCode.Services;
using Gatedge.ScanCode.Services.IServices;
using Gatedge.ScanCode.Utils;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
namespace Gatedge.ScanCode.Extension
{
///
/// 配置文件扩展类
///
public static class OptionExtension
{
///
/// 扩展金蝶云星空服务
///
/// 容器服务
/// 配置文件
public static void AddConfigureK3CloudApi(this IServiceCollection service, IConfiguration configuration)
{
var section = configuration.GetSection("Kingdee:Default");
var option = section.Get>();
service.AddScoped(s =>
{
return new K3CloudApiUtils(option);
});
}
///
/// 验证jwt
///
///
///
public static void ConfigureJwtContext(this IServiceCollection service, IConfiguration configuration)
{
var section = configuration.GetSection("Jwt:Default");
var option = section.Get();
Console.WriteLine(option);
service.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true, //是否验证Issuer
ValidIssuer = option.Issuer, //发行人Issuer
ValidateAudience = true, //是否验证Audience
ValidAudience = option.Audience, //订阅人Audience
ValidateIssuerSigningKey = true, //是否验证SecurityKey
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(option.SecretKey)), //SecurityKey
ValidateLifetime = true, //是否验证失效时间
ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值,解决服务器端时间不同步问题(秒)
RequireExpirationTime = true
};
});
service.AddSingleton(new JwtUtils(option));
}
///
/// 文件配置
///
///
///
public static void AddConfigureK3FileService(this IServiceCollection service, IConfiguration configuration)
{
var section = configuration.GetSection("FileConfig");
var option = section.Get();
service.AddScoped(s =>
{
ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
ILogger logger = new Logger(factory);
return new K3FileService(option, logger);
});
}
}
}