public async Task <IActionResult> Login([FromBody] LoginViewModel loginUser) { if (loginUser == null) { return(BadRequest("user is not set.")); } var user = await _usersService.FindByNameAsync(loginUser.Username); var result1 = await _signInManager.PasswordSignInAsync( loginUser.Username, loginUser.Password, false, lockoutOnFailure : true); if (user == null || !user.IsActive) { return(Unauthorized()); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); // _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken })); }
public async Task <IActionResult> Login([FromBody] LoginViewModel login) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _usersService.FindUserAsync(login.username, login.password); if (user == null) { return(BadRequest(Errors.AddErrorToModelState("login_failure", "Username or password is incorrect.", ModelState))); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken })); }
public async Task <JwtTokensData> CreateJwtTokens(User user) { var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(result); }
public async Task <IActionResult> Login([FromBody] User loginUser) { if (loginUser == null) { return(Ok(new { success = false, message = LoggingEvents.LOGIN_FAILED_MESSAGE })); } var user = await _userService.FindUserAsync(loginUser.Username, loginUser.Password); if (user == null || !user.IsActive) { return(Ok(new { success = false, message = LoggingEvents.LOGIN_FAILED_MESSAGE })); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken, success = true })); }
public async Task <IActionResult> Login(UserLoginDto loginUser) { if (loginUser == null) { return(BadRequest("user is not set.")); } var user = await _usersService.FindUserAsync(loginUser.Username, loginUser.Password); if (user == null || !user.IsActive) { return(Unauthorized()); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken })); }
public IActionResult Login([FromQuery] string userName, string password) { if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) { return(BadRequest(new { messageCode = -100, message = "Dữ Liệu Sai" })); } var userResult = _lesterRegistration.Login(userName, password); if (userResult.MessageCode == MessgeCodeRegistration.PasswordWrong) { return(Unauthorized(new { messageCode = MessgeCodeRegistration.PasswordWrong, message = CommonHelper.DescriptionEnum(MessgeCodeRegistration.PasswordWrong) })); } if (userResult.MessageCode != MessgeCodeRegistration.Suscess) { return(Unauthorized(new { messageCode = userResult.MessageCode, message = CommonHelper.DescriptionEnum((MessgeCodeRegistration)userResult.MessageCode) })); } var result = _tokenFactoryService.CreateJwtTokensAsync(userResult.User); _tokenStoreService.AddUserToken(userResult.User, result.RefreshTokenSerial, result.AccessToken, null); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); AccessControl.User = userResult.User; return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken })); }
public async Task <ActionResult <ClientToken> > LoginAsync([FromBody] LoginViewModel loginUser) { LoginViewModelValidator loginViewModelValidator = new LoginViewModelValidator(); var results = loginViewModelValidator.Validate(loginUser); if (!results.IsValid) { foreach (var failure in results.Errors) { _logger.LogInformation("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage); } } if (loginUser == null || !ModelState.IsValid || !results.IsValid) { return(BadRequest("user is not set.")); } var user = await _usersService.FindUserAsync(loginUser.Username, loginUser.Password); if (user == null || !user.IsActive) { return(Unauthorized()); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); return(Ok(new ClientToken { AccessToken = result.AccessToken, RefreshToken = result.RefreshToken })); }
public async Task <IActionResult> Login([FromBody] LoginModel loginUser) { if (loginUser == null) { return(BadRequest("user is not set.")); } var user = await _usersService.FindUserAsync(loginUser.Username, loginUser.Password); if (user?.F_IsActive != true) { return(Content(new AjaxResult { state = ResultType.error.ToString(), message = "登录失败!" }.ToJson())); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); //return Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken }); return(Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。", data = new { access_token = result.AccessToken, refresh_token = result.RefreshToken } }.ToJson())); }
public async Task <IActionResult> Login([FromBody] User loginUser) { if (loginUser == null) { return(BadRequest("user is not set.")); } var user = await _usersService.FindUserAsync(loginUser.Username, loginUser.Password); if (user == null || !user.IsActive) { return(Unauthorized()); } var result = await _tokenFactoryService.CreateJwtTokensAsync(user); return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken })); }
public async Task Login_In_Service() { var user = await _usersService.FindUserAsync(_rightUser.Username, _rightUser.Password); Assert.NotNull(user); Assert.True(user.IsActive); var result = await _tokenFactoryService.CreateJwtTokensAsync(user); await _tokenStoreService.AddUserTokenAsync(user, result.RefreshTokenSerial, result.AccessToken, null); await _uow.SaveChangesAsync(); Assert.NotNull(result); result.AccessToken.Should().NotBeEmpty(); result.Claims.Should().NotBeEmpty(); result.RefreshToken.Should().NotBeEmpty(); result.RefreshTokenSerial.Should().NotBeEmpty(); _antiforgery.RegenerateAntiForgeryCookies(result.Claims); }
public virtual async Task <CreateJwtTokenResult> SignInWithClaimsAsync(TUser user, IEnumerable <Claim> additionalClaims) { return(await _tokenFactoryService.CreateJwtTokensAsync(user, additionalClaims)); }