예제 #1
0
        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
            }));
        }
예제 #3
0
        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 }));
        }
예제 #5
0
        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 }));
        }
예제 #6
0
        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
            }));
        }
예제 #7
0
        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()));
        }
예제 #8
0
        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);
        }