Esempio n. 1
0
        private async Task <AuthResultDto> GenerateAuthResult(ApplicationUser user)
        {
            var roles = await _userManager.GetRolesAsync(user);

            var claimsIdentity = _jwtBuilder.GenerateClaimsIdentity(user, roles);
            var securityToken  = _jwtBuilder.GenerateSecurityToken(claimsIdentity);

            var newRefreshToken = new RefreshTokenData
            {
                RefreshToken      = Guid.NewGuid().ToString(),
                Jti               = securityToken.Id,
                CreationDate      = DateTime.UtcNow,
                ExpirationDate    = DateTime.UtcNow.AddDays(1),
                IsUsed            = false,
                IsInvalidated     = false,
                ApplicationUserId = user.Id
            };

            await _context.RefreshTokens.AddAsync(newRefreshToken);

            await _context.SaveChangesAsync();

            return(new AuthResultDto
            {
                Token = new JwtSecurityTokenHandler().WriteToken(securityToken),
                RefreshToken = newRefreshToken.RefreshToken
            });
        }
        private async Task <ClaimsIdentity> GetClaimsIdentity(string userName, string password)
        {
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                var userToVerify = await _userManager.FindByNameAsync(userName);

                if (userToVerify != null)
                {
                    bool passwordCorrect = await _userManager.CheckPasswordAsync(userToVerify, password);

                    if (passwordCorrect)
                    {
                        return(await Task.FromResult(_jwtBuilder.GenerateClaimsIdentity(userName, userToVerify.Id)));
                    }
                }
            }

            return(await Task.FromResult <ClaimsIdentity>(null));
        }