コード例 #1
0
        public async Task <AuthResponseDTO> Login(UserForAuthenticationDTO userForAuthenticationDto)
        {
            var response = await _client.PostAsJsonAsync("Account/Login", userForAuthenticationDto);

            var content = await response.Content.ReadAsStringAsync();

            var result = JsonSerializer.Deserialize <AuthResponseDTO>(content, _options);

            if (!response.IsSuccessStatusCode)
            {
                return(result);
            }

            await _localStorage.SetItemAsync("authToken", result.Token);

            await _localStorage.SetItemAsync("refreshToken", result.RefreshToken);

            ((AuthStateProvider)_authStateProvider).NotifyUserAuthentication(
                result.Token);

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "bearer", result.Token);

            return(new AuthResponseDTO {
                IsAuthSuccessful = true
            });
        }
コード例 #2
0
        public async Task <IActionResult> Login([FromBody] UserForAuthenticationDTO userForAuthenticationDTO)
        {
            _logger.LogInformation($"Login attempt for {userForAuthenticationDTO.Email}");

            var user = await _userManager.FindByNameAsync(userForAuthenticationDTO.Email);

            if (user == null || !await _userManager.CheckPasswordAsync(user,
                                                                       userForAuthenticationDTO.Password))
            {
                return(Unauthorized(new AuthResponseDTO
                {
                    ErrorMessage = "Invalid Authentication"
                }));
            }
            //check tokens and issue them
            var token = await _authenticationService.GetToken(user);

            user.RefreshToken           = _authenticationService.GenerateRefreshToken();
            user.RefreshTokenExpiryTime = DateTime.Now.AddDays(7);
            await _userManager.UpdateAsync(user);

            //set tokens on succesful login
            return(Ok(new AuthResponseDTO {
                IsAuthSuccessful = true,
                Token = token,
                RefreshToken = user.RefreshToken
            }));
        }
コード例 #3
0
        public async Task <IActionResult> Authenticate([FromBody] UserForAuthenticationDTO user)
        {
            if (!await _authManager.ValidateUser(user))
            {
                _loggerManager.LogWarn($"{nameof(Authenticate)}: Authentication failed, wrong user name or password");
                return(Unauthorized());
            }

            return(Ok(new { Token = await _authManager.CreateToken() }));
        }
コード例 #4
0
        public async Task <IActionResult> Authentication(UserForAuthenticationDTO userForAthentication)
        {
            var findedUser = await _userManager.FindByNameAsync(userForAthentication.UserName);

            var identityResult = await _signInManager.CheckPasswordSignInAsync(findedUser, userForAthentication.Password, false);

            if (!identityResult.Succeeded)
            {
                return(Unauthorized("Wrong login or password"));
            }

            (string token, User user) = await Token.CreateToken(findedUser, _config, _userManager);

            return(Ok(
                       new
            {
                token,
                user = _mapper.Map <UserForViewDTO>(user)
            }));
        }
コード例 #5
0
        public async Task <bool> ValidateUser(UserForAuthenticationDTO userForAuthenticationDTO)
        {
            _user = await _userManager.FindByNameAsync(userForAuthenticationDTO.UserName);

            return(_user != null && await _userManager.CheckPasswordAsync(_user, userForAuthenticationDTO.Password));
        }