Ejemplo n.º 1
0
        public async Task <ActionResult <TokenResult> > Refresh([FromForm] string token, [FromForm] string refreshToken)
        {
            ClaimsPrincipal principal = _tokenService.GetPrincipalFromExpiredToken(token);
            string          name      = GetNameFromClaimsPrincipal(principal);
            string          email     = GetEmailFromClaimsPrincipal(principal);

            User user = await _userManager.FindByEmailAsync(email);

            string savedRefreshToken = user.RefreshToken;

            if (savedRefreshToken != refreshToken)
            {
                throw new SecurityTokenException("Invalid refresh token");
            }

            string newJwtToken     = _tokenService.BuildToken(name, email);
            string newRefreshToken = _tokenService.GenerateRefreshToken();

            user.RefreshToken = newRefreshToken;
            await _userManager.UpdateAsync(user);

            return(Ok(new TokenResult
            {
                Token = newJwtToken,
                RefreshToken = newRefreshToken
            }));
        }
Ejemplo n.º 2
0
        public IActionResult GenerateToken([FromBody] TokenViewModel vm)
        {
            // todo call the command to validate user and then create token
            var token = _tokenService.BuildToken(vm.Email);

            return(Ok(new { token }));
        }
Ejemplo n.º 3
0
        public async Task <string> LoginWithExternalIdentifier()
        {
            var loginInfo = await _signInManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                throw new AppException(ExceptionEvent.LoginFailed);
            }

            var result = await _signInManager.ExternalLoginSignInAsync(loginInfo.LoginProvider, loginInfo.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (!result.Succeeded)
            {
                await RegisterWithLogin(loginInfo);
            }

            var user = await _signInManager.UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey);

            var roles = await _signInManager.UserManager.GetRolesAsync(user);

            return(_tokenService.BuildToken(user, roles));
        }
Ejemplo n.º 4
0
        //[AllowAnonymous]
        //[SkipInactiveUserFilter]
        public async Task <ActionResult <Jwt> > LoginAsync([FromBody] LoginVm loginVm)
        {
            var user = await _usersRepository.ReadByEmailAndPasswordAsync(loginVm.Email, Encryption.Encrypt(loginVm.Password));

            if (user == null)
            {
                _logger.LogInformation("Unauthorized user attempted log in");

                return(Unauthorized());
            }
            var tokenString = _jwtTokenService.BuildToken(user);

            _logger.LogInformation($"User {user.Email} logged in");

            return(Ok(new Jwt()
            {
                Token = tokenString
            }));
        }
Ejemplo n.º 5
0
            public async Task <LoginResult> Handle(AuthCommand request, CancellationToken cancellationToken)
            {
                User user = await _userManager.FindByEmailAsync(request.Email);

                bool validCredentials = await _userManager.CheckPasswordAsync(user, request.Password);

                if (!validCredentials)
                {
                    throw new InvalidCredentialException("Username or password is incorrect");
                }

                string newJwtToken     = _tokenService.BuildToken($"{user.FirstName} {user.LastName}", request.Email);
                string newRefreshToken = _tokenService.GenerateRefreshToken();

                user.RefreshToken = newRefreshToken;
                await _userManager.UpdateAsync(user);

                return(new LoginResult
                {
                    Token = newJwtToken,
                    RefreshToken = newRefreshToken
                });
            }
Ejemplo n.º 6
0
        public async Task <JsonResult> Login([FromBody] LoginViewModel loginViewModel)
        {
            var response = new TokenViewModel();

            var validator       = new LoginValidator();
            var validatorResult = validator.Validate(loginViewModel);

            if (validatorResult.IsValid)
            {
                var user        = serviceOfAccount.Get(a => a.UserName == loginViewModel.Login);
                var correctUser = await userManager.CheckPasswordAsync(user, loginViewModel.Password);

                if (!correctUser)
                {
                    response.Errors = new List <string>();
                    (response.Errors as List <string>).Add("Username or password is incorrect!");
                    return(Json(response));
                }

                IList <string> roles = await userManager.GetRolesAsync(user);

                var claims = new List <Claim>
                {
                    new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName),
                    new Claim(ClaimsIdentity.DefaultRoleClaimType, roles.FirstOrDefault()),
                    new Claim("UserId", user.Id)
                };

                response.Token = _tokenService.BuildToken(user.Email, claims);
            }
            else
            {
                response.Errors = validatorResult.Errors.Select(a => a.ErrorMessage).ToList();
            }
            return(Json(response));
        }
Ejemplo n.º 7
0
 private async Task <string> GenerateToken(IdentityUser user) => await tokenService.BuildToken(user);
Ejemplo n.º 8
0
        // Generates a token from the token service and returns it as a string
        private string GenerateToken(string email)
        {
            var token = _tokenService.BuildToken(email);

            return(token);
        }
Ejemplo n.º 9
0
 private string GenerateToken(User user, string role)
 {
     return(_tokenService.BuildToken(user, role));
 }
Ejemplo n.º 10
0
        public IActionResult GenerateToken([FromBody] Account account)
        {
            var token = _tokenService.BuildToken(account.Email);

            return(Ok(new { token }));
        }
Ejemplo n.º 11
0
 private string GenerateToken(ApplicationUser applicationUser)
 {
     return(_tokenService.BuildToken(applicationUser));
 }
        public IActionResult GenerateToken([FromBody] TokenViewModel vm)
        {
            var token = _tokenService.BuildToken(vm.Email);

            return(Ok(new { token }));
        }
Ejemplo n.º 13
0
        public IActionResult GeneraToken(ModelToken modelToken)
        {
            var Token = TokenServices.BuildToken(modelToken.Email);

            return(Ok(new { Token }));
        }
Ejemplo n.º 14
0
        public IActionResult GenerateToken([FromBody] DisplayPersonModel vm)
        {
            var token = _tokenService.BuildToken(vm.EmailAddreess);

            return(Ok(new { token }));
        }
Ejemplo n.º 15
0
        public string GenerateToken(string email)
        {
            var token = tokenService.BuildToken(email);

            return(token);
        }