1
This commit is contained in:
30
Gatedge.ScanCode/Extension/K3CloudExtension.cs
Normal file
30
Gatedge.ScanCode/Extension/K3CloudExtension.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Gatedge.K3Cloud.Utils.Model.K3Request;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Gatedge.ScanCode.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// 金蝶云星空扩展方法
|
||||
/// </summary>
|
||||
public static class K3CloudExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过验证信息获取UserInfo
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static LoginInfo GetLoginInfoByClaimsPrincipal(this ClaimsPrincipal user)
|
||||
{
|
||||
var logInfo = new LoginInfo()
|
||||
{
|
||||
UserName = user.FindFirstValue("UserName"),
|
||||
LCId = Convert.ToInt32(user.FindFirstValue("LCId")),
|
||||
OrgNum = user.FindFirstValue("orgNum"),
|
||||
DBID = user.FindFirstValue("DBID"),
|
||||
ServerUrl = user.FindFirstValue("ServerUrl"),
|
||||
};
|
||||
return logInfo;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
82
Gatedge.ScanCode/Extension/OptionExtension.cs
Normal file
82
Gatedge.ScanCode/Extension/OptionExtension.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Gatedge.ScanCode/Extension/ServiceExtension.cs
Normal file
38
Gatedge.ScanCode/Extension/ServiceExtension.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Gatedge.ScanCode.Services;
|
||||
using Gatedge.ScanCode.Services.IServices;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Gatedge.ScanCode.Extension
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务扩展
|
||||
/// </summary>
|
||||
public static class ServiceExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加MVC服务,并配置默认的输出格式为JSON
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
public static void ConfigureServices(this IServiceCollection services)
|
||||
{
|
||||
// 添加MVC服务,并配置默认的输出格式为JSON
|
||||
services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
// 可以在这里配置JSON序列化选项,例如日期格式、驼峰命名等
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
options.JsonSerializerOptions.WriteIndented = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册Bean服务
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
public static void ConfigureCore(this IServiceCollection service)
|
||||
{
|
||||
service.AddSingleton<IAuthorizationMiddlewareResultHandler, Middleware.AuthorizationMiddleware>();
|
||||
service.AddTransient<IAccountService, AccountService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user