Esempio n. 1
0
        public JwtTokenResult GenerateJwtToken(ApplicationUser user)
        {
            List <Claim> claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.NameIdentifier, user.Id)
            };

            SymmetricSecurityKey key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtKey"]));
            SigningCredentials   credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            DateTime             expires     = DateTime.Now.AddDays(Convert.ToDouble(_configuration["JwtExpireDays"]));

            JwtSecurityToken token = new JwtSecurityToken(
                _configuration["JwtIssuer"],
                _configuration["JwtIssuer"],
                claims,
                expires: expires,
                signingCredentials: credentials
                );
            JwtTokenResult result = new JwtTokenResult
            {
                UserName = user.UserName,
                FullName = user.FullName,
                Email    = user.Email,
                Token    = new JwtSecurityTokenHandler().WriteToken(token)
            };

            return(result);
        }
Esempio n. 2
0
        private JwtTokenResult GetTokenResponse(ApplicationUser user)
        {
            var            token  = GetToken(user);
            JwtTokenResult result = new JwtTokenResult
            {
                AccessToken     = token,
                ExpireInSeconds = _configuration.GetValue <int>("Tokens:Lifetime"),
                UserId          = user.Id
            };

            return(result);
        }
Esempio n. 3
0
        public async Task <ActionResult <JwtTokenResult> > Token([FromQuery] Login login)
        {
            try
            {
                var user = await _userManager.FindByEmailAsync(login.Email);

                var result = await _signInManager.CheckPasswordSignInAsync(user, login.Password, false);

                var roleClaims = (await _userManager.GetRolesAsync(user)).Select(role => new Claim(ClaimTypes.Role, role));

                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, login.Email),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    new Claim(JwtRegisteredClaimNames.UniqueName, login.Email),
                    new Claim(BuyerClaim.BuyerId, user.BuyerId.ToString()),
                };

                claims = claims.Concat(roleClaims).ToArray();

                if (result.Succeeded)
                {
                    var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtInfo.Key));
                    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                    var token = new JwtSecurityToken(JwtInfo.Issuer, JwtInfo.Audience, claims, expires: DateTime.Now.AddHours(1), signingCredentials: creds);

                    var tokenResult = new JwtTokenResult
                    {
                        Token = new JwtSecurityTokenHandler().WriteToken(token)
                    };

                    return(tokenResult);
                }

                return(BadRequest());
            }
            catch (Exception exception)
            {
                _logger.LogError($"Error occured during creating token. Exception: {exception.Message}");
                return(BadRequest());
            }
        }