using Falcon.SugarApi.Encryption;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
namespace Falcon.SugarApi.JWT
{
///
/// 服务扩展
///
public static class IServiceCollectionExtend
{
///
/// 注册Falcon.SqlSugar.JWT
///
/// 服务集合
/// 服务集合
public static IServiceCollection AddFalconJWT(this IServiceCollection services)
=> services.AddFalconJWT(null);
///
/// 注册Falcon.SqlSugar.JWT.JwtContext,并配置相关参数
/// 消费端通过JwtContext注入
///
/// 服务集合
/// 参数创建器
/// 服务集合
public static IServiceCollection AddFalconJWT(this IServiceCollection services, Action? OptionBuilder) {
services.AddSingleton(sp => {
var option = new JwtContext();
var jtb = sp.GetService();
if (jtb == null) {
var aesc = sp.GetService() ?? new AESConfig();
jtb = new AESTokenbuilder(aesc, option);
}
option.JwtTokenBuilder = jtb;
var ulj = sp.GetService();
if (ulj != null) {
option.UserLogin = ulj;
}
OptionBuilder?.Invoke(option);
if (option.UserLogin == null) {
throw new Exception("必须为JwtOptions提供UserLogin属性!");
}
if (option.JwtTokenBuilder == null) {
throw new Exception("必须为JwtOptions提供JwtTokenBuilder属性!");
}
return option;
});
return services;
}
}
}