public async Task <LogInResponseModel> LogInAsync(LogInRequestModel logInRequestModel) { var response = new LogInResponseModel { IsSuccessful = false }; UserData userData = await applicationUserRepository.FindByEmailAsync(logInRequestModel.Email.Normalize()); if (userData == null) { response.Message = "Account with this email doesn`t exists"; } else if (!await applicationUserRepository.CheckPasswordAsync( logInRequestModel.Email, logInRequestModel.Password)) { response.Message = "Wrong Password"; } else { string token = javascriptWebTokenFactory.Create(userData.Id); var sessionDto = new SessionData { UserId = userData.Id, Token = token }; await sessionRepository.CreateAsync(sessionDto); response.Token = token; response.IsSuccessful = true; } return(response); }
public async Task <LogInResponseModel> SingInAsync(SingInModel model) { LogInResponseModel logInResponse = new LogInResponseModel(); List <Claim> accessClaims = new List <Claim>(); List <Claim> refreshClaims = new List <Claim>(); string accessToken; string refreshToken; User user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { logInResponse.IsValid = false; logInResponse.Message.Add(_userNotFoundMsg); return(logInResponse); } if (!user.EmailConfirmed) { logInResponse.IsValid = false; logInResponse.Message.Add(_emailNOTConfirmedMsg); return(logInResponse); } IList <string> roles = await _userManager.GetRolesAsync(user); logInResponse.Role = roles.FirstOrDefault(); logInResponse.User = user.NormalizedUserName; bool confirm = await _userManager.CheckPasswordAsync(user, model.Password); if (!confirm) { logInResponse.IsValid = false; logInResponse.Message.Add(_wrongPass); return(logInResponse); } accessClaims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); accessClaims.Add(new Claim(ClaimTypes.Email, user.Email)); accessClaims.Add(new Claim(ClaimTypes.Hash, user.PasswordHash)); accessClaims.Add(new Claim(ClaimTypes.Role, logInResponse.Role)); accessToken = GenerateAccesToken(accessClaims); logInResponse.AccessToken = accessToken; refreshClaims.Add(new Claim(ClaimTypes.Authentication, accessToken)); refreshClaims.Add(new Claim(ClaimTypes.Email, user.Email)); refreshToken = GenerateRefreshToken(refreshClaims); logInResponse.RefreshToken = refreshToken; return(logInResponse); }
public async Task LoginTest() { LogInRequestModel logInRequestModel = CreateLogInRequestModel(); LogInResponseModel expected = CreateLogInResponseModel(); SetupAccountServiceLoginMock(logInRequestModel, expected); var actual = (JsonResult)await accountApiController.Login(logInRequestModel); Assert.AreEqual(expected, actual.Value); }
public async Task <LogInResponseModel> SingInAsync([FromBody] SingInModel model) { LogInResponseModel logInResponse = await _accountService.SingInAsync(model); return(logInResponse); }
private void SetupAccountServiceLoginMock(LogInRequestModel logInRequestModel, LogInResponseModel logInResponseModel) => accountServiceMock .Setup(service => service.LogInAsync(logInRequestModel)) .ReturnsAsync(logInResponseModel);