diff --git a/Apimanager_backend/Apimanager_backend.csproj b/Apimanager_backend/Apimanager_backend.csproj
index 2d17395..80575c2 100644
--- a/Apimanager_backend/Apimanager_backend.csproj
+++ b/Apimanager_backend/Apimanager_backend.csproj
@@ -8,16 +8,14 @@
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
+
-
-
-
-
diff --git a/Apimanager_backend/Config/ServiceCollectionExtensions.cs b/Apimanager_backend/Config/ServiceCollectionExtensions.cs
index 28a5528..4864289 100644
--- a/Apimanager_backend/Config/ServiceCollectionExtensions.cs
+++ b/Apimanager_backend/Config/ServiceCollectionExtensions.cs
@@ -1,5 +1,10 @@
using Apimanager_backend.Services;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.IdentityModel.Tokens;
+using StackExchange.Redis;
+using System.ComponentModel;
using System.Runtime.CompilerServices;
+using System.Text;
namespace Apimanager_backend.Config
{
@@ -8,7 +13,35 @@ namespace Apimanager_backend.Config
public static IServiceCollection AddAllService(this IServiceCollection services,IConfiguration configuration)
{
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
+ services.AddJWTService(configuration);
services.AddScoped();
+ services.AddScoped();
+ services.AddSingleton();
+ services.AddSingleton();
+ return services;
+ }
+ public static IServiceCollection AddJWTService(this IServiceCollection services,IConfiguration configuration)
+ {
+ var jwtSettings = configuration.GetSection("JwtSettings");
+ var key = Encoding.ASCII.GetBytes(jwtSettings["Secret"]);
+ // JWT配置
+ services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
+ .AddJwtBearer(options =>
+ {
+ options.TokenValidationParameters = new TokenValidationParameters
+ {
+ ValidateIssuer = true,
+ ValidateAudience = true,
+ ValidateLifetime = true,
+ ValidateIssuerSigningKey = true,
+ ValidIssuer = jwtSettings["Issuer"],
+ ValidAudience = jwtSettings["Audience"],
+ IssuerSigningKey = new SymmetricSecurityKey(key)
+ };
+ });
+
+ //redis配置
+ services.AddSingleton(ConnectionMultiplexer.Connect(configuration["Redis:ConnectionString"]));
return services;
}
}
diff --git a/Apimanager_backend/Controllers/AuthController.cs b/Apimanager_backend/Controllers/AuthController.cs
new file mode 100644
index 0000000..a662105
--- /dev/null
+++ b/Apimanager_backend/Controllers/AuthController.cs
@@ -0,0 +1,112 @@
+using Apimanager_backend.Dtos;
+using Apimanager_backend.Exceptions;
+using Apimanager_backend.Models;
+using Apimanager_backend.Services;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Apimanager_backend.Controllers
+{
+ [Route("api/[controller]/[action]")]
+ [ApiController]
+ public class AuthController : ControllerBase
+ {
+ private readonly IAuthService authService;
+ private readonly ITokenService tokenService;
+ private readonly IRefreshTokenService refreshTokenService;
+ private readonly IUserService userService;
+ public AuthController(IAuthService authService, ITokenService tokenService, IRefreshTokenService refreshTokenService,IUserService userService)
+ {
+ this.authService = authService;
+ this.tokenService = tokenService;
+ this.refreshTokenService = refreshTokenService;
+ this.userService = userService;
+ }
+ ///
+ /// 用户登录控制器
+ ///
+ /// 登录信息
+ /// 通用返回信息格式
+ [HttpPost]
+ public async Task>> Login([FromBody] UserLoginDto dto)
+ {
+ try
+ {
+ UserInfoDto user = await authService.LoginAsync(dto.UserName, dto.Password);
+ //生成token
+ string token = tokenService.GenerateAccessToken(user.Id.ToString(),user.Roles);
+ //生成refreshtoken
+ string refreshToken = await refreshTokenService.CreateRefereshTokenAsync(user.Id.ToString());
+ var responseInfo = new ResponseBase(
+ code: 2000,
+ message: "Login successful",
+ data: new LoginResponseDto
+ {
+ UserInfo = user,
+ Token = token,
+ RefreshToken = refreshToken
+ }
+ );
+ return Ok(responseInfo);
+ }
+ catch (BaseException e)
+ {
+
+ //错误时,构建错误信息对象
+ var responseInfo = new ResponseBase