public async Task <ActionResult> Login([FromBody] LoginRequest request) { if (!ModelState.IsValid) { return(BadRequest()); } if (!await _userService.IsValidUserCredentialsAsync(request.UserName, request.Password).ConfigureAwait(false)) { return(Unauthorized()); } var role = _userService.GetUserRole(request.UserName); var claims = new[] { new Claim(ClaimTypes.Name, request.UserName), new Claim(ClaimTypes.Role, role) }; var jwtResult = _jwtAuthManager.GenerateTokens(request.UserName, claims, DateTime.Now); _logger.LogInformation($"User [{request.UserName}] logged in the system."); return(Ok(new LoginResult { UserName = request.UserName, Role = role, AccessToken = jwtResult.AccessToken, RefreshToken = jwtResult.RefreshToken.TokenString })); }
public async Task <IActionResult> GetTokenAsync(LoginRequest loginModel) { if (!ModelState.IsValid) { _logger.LogInformation($"The login model is not valid for [{loginModel.UserName}] user"); return(BadRequest(ModelState)); } var userCredentialsValidity = await _userService.IsValidUserCredentialsAsync(loginModel.UserName, loginModel.Password); if (userCredentialsValidity is false) { _logger.LogInformation($"Incorrectly passed user [{loginModel.UserName}] login and [{loginModel.Password}] password"); return(Unauthorized()); } var user = await _userService.GetUserAsync(login : loginModel.UserName, password : loginModel.Password); var role = _userService.DetectUserRole(login: loginModel.UserName); // TODO: доработать набор утверждений о пользователе // оставить только имя/роль/идентификатор var claims = new[] { new Claim(ClaimsIdentity.DefaultNameClaimType, loginModel.UserName), new Claim(ClaimTypes.NameIdentifier, loginModel.UserName), new Claim(ClaimsIdentity.DefaultRoleClaimType, role), new Claim("UserId", $"{user.Id}") }; _logger.LogInformation($"Start generating of the access token for [{loginModel.UserName}] user"); var jwtAuthResult = _jwtAuthManager.GenerateTokens(loginModel.UserName, claims, DateTime.Now); _logger.LogInformation($"The access token is created for [{loginModel.UserName}] user"); _logger.LogDebug($"The access token: [{jwtAuthResult.AccessToken}]"); _logger.LogInformation($"The [{loginModel.UserName}] user is logged in the system"); return(Ok(new LoginResult { Role = role, UserName = loginModel.UserName, ExpiresAt = jwtAuthResult.ExpiresAt, AccessToken = jwtAuthResult.AccessToken, RefreshToken = jwtAuthResult.RefreshToken.TokenString })); }
public async Task <LoginResultDto> Login(LoginRequestDto loginRequestDto) { var user = await _userManager.FindByNameAsync(loginRequestDto.UserName); //var role = _userService.GetUserRole(request.UserName); var roles = await _userManager.GetRolesAsync(user); var claims = new List <Claim>(); claims.Add(new Claim(ClaimTypes.Name, loginRequestDto.UserName)); if (roles != null) { foreach (var role in roles) { claims.Add(new Claim(ClaimTypes.Role, role)); } } //var claims = new[] //{ // new Claim(ClaimTypes.Name,loginRequestDto.UserName), // new Claim(ClaimTypes.Role, roles.ToString()) //}; var jwtResult = _jwtAuthService.GenerateTokens(loginRequestDto.UserName, claims.ToArray(), DateTime.Now); _logger.LogInformation($"User [{loginRequestDto.UserName}] logged in the system."); return(new LoginResultDto { UserName = loginRequestDto.UserName, Roles = roles?.ToArray(), AccessToken = jwtResult.AccessToken, RefreshToken = jwtResult.RefreshToken.TokenString }); }
public async Task <IActionResult> GetTokenAsync(LoginRequest loginModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!await _userService.IsValidUserCredentialsAsync(loginModel.UserName, loginModel.Password).ConfigureAwait(false)) { return(Unauthorized()); } var user = await _userRepository.Get(loginModel.UserName, loginModel.Password); var role = _userService.GetUserRole(loginModel.UserName); var claims = new[] { new Claim(ClaimTypes.Name, loginModel.UserName), new Claim(ClaimTypes.Role, role), new Claim("UserId", $"{user.Id}") }; var jwtResult = _jwtAuthManager.GenerateTokens(loginModel.UserName, claims, DateTime.Now); _logger.LogInformation($"User [{loginModel.UserName}] logged in the system."); return(Ok(new LoginResult { UserName = loginModel.UserName, Role = role, //ExpiresAt = jwtResult.ExpiresAt.HasValue ? jwtResult.ExpiresAt.Value.Millisecond : 0, ExpiresAt = jwtResult.ExpiresAt, AccessToken = jwtResult.AccessToken, RefreshToken = jwtResult.RefreshToken.TokenString })); }