Skip to content

Commit

Permalink
Login LoginRequest data
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Jun 16, 2024
1 parent f2c06ab commit a96d4ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ TDbContext _dbContext

if (!MiniAuthOptions.DisableMiniAuthLogin)
{
endpoints.MapPost($"/{MiniAuthOptions.RoutePrefix}/login", async (HttpContext context
, TDbContext _dbContext
, SignInManager<TIdentityUser> signInManager
, UserManager<TIdentityUser> _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<string>("username");
var password = root.GetProperty<string>("password");
var remember = root.GetProperty<bool>("remember");
UserManager<TIdentityUser> _userManager = sp.GetRequiredService<UserManager<TIdentityUser>>();
TDbContext _dbContext = sp.GetRequiredService<TDbContext>();
SignInManager<TIdentityUser> signInManager = sp.GetRequiredService<SignInManager<TIdentityUser>>();

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;
Expand All @@ -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()
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/MiniAuth.IdentityAuth/Models/LoginRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit a96d4ff

Please sign in to comment.