83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 配置文件扩展类
|
|
/// </summary>
|
|
public static class OptionExtension
|
|
{
|
|
|
|
/// <summary>
|
|
/// 扩展金蝶云星空服务
|
|
/// </summary>
|
|
/// <param name="service">容器服务</param>
|
|
/// <param name="configuration">配置文件</param>
|
|
public static void AddConfigureK3CloudApi(this IServiceCollection service, IConfiguration configuration)
|
|
{
|
|
var section = configuration.GetSection("Kingdee:Default");
|
|
var option = section.Get<List<K3CloudOption>>();
|
|
service.AddScoped(s =>
|
|
{
|
|
return new K3CloudApiUtils(option);
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证jwt
|
|
/// </summary>
|
|
/// <param name="service"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void ConfigureJwtContext(this IServiceCollection service, IConfiguration configuration)
|
|
{
|
|
var section = configuration.GetSection("Jwt:Default");
|
|
var option = section.Get<JwtOption>();
|
|
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));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 文件配置
|
|
/// </summary>
|
|
/// <param name="service"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddConfigureK3FileService(this IServiceCollection service, IConfiguration configuration)
|
|
{
|
|
var section = configuration.GetSection("FileConfig");
|
|
var option = section.Get<FileOption>();
|
|
service.AddScoped<IK3FileService>(s =>
|
|
{
|
|
ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
|
|
ILogger<K3FileService> logger = new Logger<K3FileService>(factory);
|
|
return new K3FileService(option, logger);
|
|
});
|
|
}
|
|
}
|
|
}
|