diff --git a/src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs b/src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs index 3fabccb..0e4ec61 100644 --- a/src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs +++ b/src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs @@ -57,22 +57,20 @@ TDbContext _dbContext if (!MiniAuthOptions.DisableMiniAuthLogin) { - endpoints.MapPost($"/{MiniAuthOptions.RoutePrefix}/login", async (HttpContext context - , TDbContext _dbContext - , SignInManager signInManager - , UserManager _userManager + endpoints.MapPost($"/{MiniAuthOptions.RoutePrefix}/login", async ( + [FromBody] LoginRequest login + , [FromServices] IServiceProvider sp + , HttpContext context ) => { - JsonDocument bodyJson = await GetBodyJson(context); - var root = bodyJson.RootElement; - var userName = root.GetProperty("username"); - var password = root.GetProperty("password"); - var remember = root.GetProperty("remember"); + UserManager _userManager = sp.GetRequiredService>(); + TDbContext _dbContext = sp.GetRequiredService(); + SignInManager signInManager = sp.GetRequiredService>(); if (MiniAuth.MiniAuthOptions.AuthenticationType == MiniAuthOptions.AuthType.BearerJwt) { - var user = await _dbContext.Users.FirstOrDefaultAsync(f => f.UserName == userName); - if (!(user != null && await _userManager.CheckPasswordAsync((TIdentityUser)user, password))) + var user = await _dbContext.Users.FirstOrDefaultAsync(f => f.UserName == login.username); + if (!(user != null && await _userManager.CheckPasswordAsync((TIdentityUser)user, login.password))) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; return; @@ -89,7 +87,6 @@ TDbContext _dbContext claims.Add(new Claim(ClaimTypes.Role, item)); claims.Add(new Claim("sub", user.UserName)); - var secretkey = MiniAuthOptions.JWTKey; var credentials = new SigningCredentials(secretkey, SecurityAlgorithms.HmacSha256); var tokenDescriptor = new SecurityTokenDescriptor() @@ -115,7 +112,7 @@ TDbContext _dbContext } else { - var result = await signInManager.PasswordSignInAsync(userName, password, remember, lockoutOnFailure: false); + var result = await signInManager.PasswordSignInAsync(login.username, login.password, login.remember, lockoutOnFailure: false); if (result.Succeeded) { var newToken = Guid.NewGuid().ToString(); diff --git a/src/MiniAuth.IdentityAuth/Models/LoginRequest.cs b/src/MiniAuth.IdentityAuth/Models/LoginRequest.cs new file mode 100644 index 0000000..8abb357 --- /dev/null +++ b/src/MiniAuth.IdentityAuth/Models/LoginRequest.cs @@ -0,0 +1,10 @@ +namespace MiniAuth.IdentityAuth.Models +{ + public sealed class LoginRequest + { + public string username { get; init; } + + public string password { get; init; } + public bool remember { get; init; } + } +}