Beispiel #1
0
        public async Task <UserResponseLogin> GenerateJWT(string _email)
        {
            IdentityUser user = await __userManager.FindByEmailAsync(_email);

            IList <Claim> claims = await __userManager.GetClaimsAsync(user);

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

            claims.Add(new Claim(JwtRegisteredClaimNames.Sub, user.Id));
            claims.Add(new Claim(JwtRegisteredClaimNames.Email, user.Email));
            claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
            claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, ToUnixEpochDate(DateTime.UtcNow).ToString()));
            claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, ToUnixEpochDate(DateTime.UtcNow).ToString(), ClaimValueTypes.Integer64));

            foreach (string userRole in roles)
            {
                claims.Add(new Claim("role", userRole));
            }

            ClaimsIdentity identityClaims = new ClaimsIdentity();

            identityClaims.AddClaims(claims);

            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

            byte[] key = Encoding.ASCII.GetBytes(__appSettings.Secret);

            SecurityToken token = tokenHandler.CreateToken(new SecurityTokenDescriptor {
                Issuer             = __appSettings.Emitter,
                Audience           = __appSettings.ValidAt,
                Subject            = identityClaims,
                Expires            = DateTime.UtcNow.AddHours(__appSettings.ExpirationHours),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            });

            string            encodedToken = tokenHandler.WriteToken(token);
            UserResponseLogin response     = new UserResponseLogin()
            {
                AccessToken = encodedToken,
                ExpiresIn   = TimeSpan.FromHours(__appSettings.ExpirationHours).TotalSeconds,
                UserToken   = new UserToken()
                {
                    Id     = user.Id,
                    Email  = user.Email,
                    Claims = claims.Select(w => new UserClaim {
                        Type  = w.Type,
                        Value = w.Value
                    })
                }
            };

            return(response);
        }
Beispiel #2
0
        private async Task RealizaLogin(UserResponseLogin user)
        {
            var token = GetTokenFormated(user.AccessToken);

            var claims = new List <Claim>();

            claims.Add(new Claim("JWT", user.AccessToken));
            claims.AddRange(token.Claims);

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(60),
                IsPersistent = true
            };

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                          new ClaimsPrincipal(claimsIdentity), authProperties);
        }