[feature] 建立鉴权项目结构

This commit is contained in:
lichx
2024-10-30 17:52:22 +08:00
parent 8c019a3159
commit d33ff74a51
23 changed files with 694 additions and 0 deletions
@@ -0,0 +1,24 @@
using SiteManagementSystem_SoftwareEngineering_.Model;
using Microsoft.AspNetCore.Mvc;
namespace SiteManagementSystem_SoftwareEngineering_.Extension
{
public static class ControllerBaseExtension
{
public static IActionResult Success(
this ControllerBase controller,
string msg = "",
object? data = null
) => ApiResponse.Success(msg, data);
public static IActionResult Forbidden(
this ControllerBase controller,
string msg = "",
object? data = null
)=> ApiResponse.Forbidden(msg, data);
public static IActionResult Fail(
this ControllerBase controller,
string msg = "",
object? data = null
) => ApiResponse.Fail(msg, data);
}
}
@@ -0,0 +1,42 @@
using IwutMail.Model;
using SiteManagementSystem_SoftwareEngineering_.Factory;
using SiteManagementSystem_SoftwareEngineering_.Interface;
using SiteManagementSystem_SoftwareEngineering_.Model;
using SiteManagementSystem_SoftwareEngineering_.Service;
using static SiteManagementSystem_SoftwareEngineering_.Service.UserManagerService;
namespace SiteManagementSystem_SoftwareEngineering_.Extension
{
public static class IServiceCollectionExtension
{
public static IServiceCollection AddTokenFactory(
this IServiceCollection services,
Action<TokenFactoryConfiguration> options
)
{
var config = new TokenFactoryConfiguration();
options(config);
if (config.Audience is null)
throw new ArgumentNullException(nameof(config.Audience) + "can not be null.");
if (config.Issuer is null)
throw new ArgumentNullException(nameof(config.Issuer) + "can not be null.");
if (config.SigningKey is null)
throw new ArgumentNullException(nameof(config.SigningKey) + "can not be null.");
services.AddScoped<ITokenFactory, TokenFactory>(_ => new TokenFactory(config));
return services;
}
public static IServiceCollection AddUserManager(this IServiceCollection services, Action<SecretConfig> options)
{
var config = new SecretConfig();
options(config);
if (config.HashSalt is null) throw new ArgumentNullException(nameof(config.HashSalt) + "can not be null");
services.AddScoped<IUserManageService, UserManageService>(services =>
{
var logger = services.GetRequiredService<ILogger<UserManageService>>();
var storageService= services.GetRequiredService<SQLService>();
return new UserManageService(storageService, logger, config.HashSalt);
});
return services;
}
}
}