public async Task <IActionResult> SignIn(AppUserLoginDto appUserLoginDto) { var appUser = await _appUserService.FindByUserName(appUserLoginDto.UserName); if (appUser == null) { return(BadRequest("Username or password is incorrect.")); } else { if (await _appUserService.CheckPassword(appUserLoginDto)) { var roles = await _appUserService.GetRolesByUserName(appUserLoginDto.UserName); var token = _jwtService.GenerateJwtToken(appUser, roles); JwtAccessToken jwtAccessToken = new JwtAccessToken(); jwtAccessToken.Token = token; return(Created("", jwtAccessToken)); } else { return(BadRequest("Username or password is incorrect.")); } } }
RefreshTokenAsync_WhenUserWithPassedTokenFoundButTokenNotActiveAndUserUpdateSuccess_ShouldGenerateNewRefreshToken() { var tokenPassed = Fixture.Create <string>(); var listOfToken = Fixture.Build <RefreshToken>() .With(w => w.Token, tokenPassed + "1") .CreateMany().ToList(); var user = Fixture.Build <User>().With(w => w.RefreshTokens, listOfToken).Create(); _mockUserRepository.Setup(e => e.GetUserWithActiveRefreshToken(tokenPassed)) .ReturnsAsync(user); _mockRefreshTokenRepository.Setup(r => r.FilterOneAsync(It.IsAny <Expression <Func <RefreshToken, bool> > >())) .ReturnsAsync(new RefreshToken()); _mockUserManager.Setup(e => e.UpdateAsync(It.IsAny <User>())) .ReturnsAsync(IdentityResult.Success); var token = new JwtAccessToken(); _mockFactory.Setup(e => e.GenerateEncodedAccessToken(It.IsAny <User>())) .ReturnsAsync(token); var newRefreshToken = new RefreshToken(); _mockFactory.Setup(e => e.GenerateRefreshToken(32)) .Returns(newRefreshToken); var response = await sut.RefreshTokenAsync(tokenPassed); response.Token.Should().Be(token.Token); response.RefreshToken.Should().Be(newRefreshToken.Token); response.ExpiredAt.Should().Be((newRefreshToken.Expires - DateTimeOffset.UtcNow).Seconds); }
public async Task <IActionResult> SignIn(AppUserLoginDto appUserLoginDto) { // userName => var mı ? // password => eşleniyor mu ? var appUser = await _appUserService.FindByUserName(appUserLoginDto.UserName); if (appUser == null) { return(BadRequest("Kullanıcı adı veya şifre hatalı.")); } else { if (await _appUserService.CheckPassword(appUserLoginDto)) { var roles = await _appUserService.GetRolesByUserName(appUserLoginDto.UserName); var token = _jwtService.GenereateJwtToken(appUser, roles); JwtAccessToken jwtAccessToken = new JwtAccessToken(); jwtAccessToken.Token = token; return(Created("", jwtAccessToken)); } return(BadRequest("Kullanıcı adı veya şifre hatalı")); } }
//[ValidModel] public async Task <IActionResult> SignIn([FromBody] AppUserLoginDto appUserLoginDto) { try { var appUser = await _appUserService.FindByUsername(appUserLoginDto.Username); if (appUser == null) { return(BadRequest("Kullanıcı adı veya şifre hatalı")); } else { if (await _appUserService.CheckPassword(appUserLoginDto)) { var roles = await _appUserService.GetRolesByUsername(appUserLoginDto.Username); var token = _jwtService.GenerateJwt(appUser, roles); JwtAccessToken jwtAccessToken = new JwtAccessToken(); jwtAccessToken.Token = token; return(Created("", jwtAccessToken)); } return(BadRequest("Kullanıcı adı veya şifre hatalı")); } } catch (Exception exception) { return(BadRequest(exception.Message)); } }
public void RemoveAccessToken(JwtAccessToken accessToken) { if (accessToken != null) { _localDBContext.AccessTokens.Remove(accessToken); _localDBContext.SaveChanges(); } }
public void SaveAccessToken(JsonWebToken accessToken, int userId) { // create new token then add it! var newToken = new JwtAccessToken { JWTToken = accessToken.Token, UserId = userId, ExpiresAt = accessToken.ExpiresAt }; _localDBContext.AccessTokens.Add(newToken); _localDBContext.SaveChanges(); }
public async Task LoginAsync_WhenUserWithRefreshTokenNotActiveAndUSerUpdatedSuccessfully_ShouldGenerateNewRefreshToken() { var tokenExchangeRequest = new TokenExchangeRequest() { Password = "******", Email = "email", }; var liStOfTokens = Fixture.Build <RefreshToken>() .With(e => e.Expires, DateTimeOffset.MaxValue) .With(e => e.Revoked) .CreateMany().ToList(); var user = Fixture.Build <User>(). With(w => w.EmailConfirmed, true). With(w => w.LockoutEnd, DateTimeOffset.MinValue). With(e => e.RefreshTokens, liStOfTokens). Create(); _mockUserManager.Setup(e => e.FindByEmailAsync(tokenExchangeRequest.Email)) .ReturnsAsync(user); _mockUserManager.Setup(e => e.CheckPasswordAsync(user, tokenExchangeRequest.Password)) .ReturnsAsync(true); var token = new JwtAccessToken(); _mockFactory.Setup(e => e.GenerateEncodedAccessToken(user)) .ReturnsAsync(token); var refreshToken = new RefreshToken(); _mockFactory.Setup(e => e.GenerateRefreshToken(32)) .Returns(refreshToken); _mockUserManager.Setup(e => e.UpdateAsync(user)) .ReturnsAsync(IdentityResult.Success); _mockRefreshTokenRepository.Setup(e => e.FilterOneAsync(It.IsAny <Expression <Func <RefreshToken, bool> > >())) .ReturnsAsync((RefreshToken)null); var response = await sut.LoginAsync(tokenExchangeRequest); response.RefreshToken.Should().BeEquivalentTo(refreshToken.Token); response.Token.Should().BeEquivalentTo(token.Token); response.ExpiredAt.Should().Be((refreshToken.Expires - DateTimeOffset.UtcNow).Seconds); }
public async Task <IActionResult> SignIn([FromQuery] AppUserLoginDto appUserLoginDto) { var appUser = await this.appUserService.FindByUserNameAsync(appUserLoginDto.UserName); if (appUser == null) { return(BadRequest("Username or password is invalid")); } if (await this.appUserService.CheckPasswordAsync(appUserLoginDto)) { var roles = await this.appUserService.GetRolesByUserName(appUserLoginDto.UserName); var token = this.jwtService.GenerateJwt(appUser, roles); JwtAccessToken Token = new JwtAccessToken { Token = token }; return(Created("", Token)); } return(BadRequest("Username or password is invalid")); }
public async Task LoginAsync_WhenUserWithActiveRefreshTokenPassed_ShouldReturnAuthResponse() { var tokenExchangeRequest = new TokenExchangeRequest() { Password = "******", Email = "email", }; var liStOfTokens = Fixture.Build <RefreshToken>() .With(e => e.Expires, DateTimeOffset.MaxValue) .With(e => e.Revoked, (DateTimeOffset?)null) .CreateMany().ToList(); var user = Fixture.Build <User>(). With(w => w.EmailConfirmed, true). With(w => w.LockoutEnd, DateTimeOffset.MinValue). With(e => e.RefreshTokens, liStOfTokens). Create(); var activeToken = liStOfTokens.FirstOrDefault(e => e.IsActive); _mockUserManager.Setup(e => e.FindByEmailAsync(tokenExchangeRequest.Email)) .ReturnsAsync(user); _mockUserManager.Setup(e => e.CheckPasswordAsync(user, tokenExchangeRequest.Password)) .ReturnsAsync(true); var token = new JwtAccessToken(); _mockFactory.Setup(e => e.GenerateEncodedAccessToken(user)) .ReturnsAsync(token); _mockRefreshTokenRepository.Setup(e => e.FilterOneAsync(It.IsAny <Expression <Func <RefreshToken, bool> > >())) .ReturnsAsync(activeToken); var response = await sut.LoginAsync(tokenExchangeRequest); Debug.Assert(activeToken != null, nameof(activeToken) + " != null"); response.RefreshToken.Should().BeEquivalentTo(activeToken.Token); response.Token.Should().BeEquivalentTo(token.Token); response.ExpiredAt.Should().Be((activeToken.Expires - DateTimeOffset.UtcNow).Seconds); }
public async Task <IActionResult> Signin(UserLoginDto userLoginDto) { var user = await _userManager.FindByEmail(userLoginDto.Email); if (user == null) { return(BadRequest("Kullanıcı adı Yada Şifre Hatalı")); } else { if (await _userManager.CheckPassword(userLoginDto)) { var roles = await _userManager.GetRolesByEmail(userLoginDto.Email); var token = _jwtManager.TokenCreate(user, roles); JwtAccessToken jwtAccessToken = new JwtAccessToken { Token = token }; return(Created("", jwtAccessToken)); } return(BadRequest("Kullanıcı Adı Veya Şifre Hatalı")); } }
public TokenResponse(bool isSuccess, string message, JwtAccessToken token) : base(isSuccess, message) { Token = token; }