Esempio n. 1
0
        //Создание токена
        private string GenerateJSONWebToken(User user)
        {
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Jwt:Keyqwertyuytrewertyuiqwe"));
            var credintalis = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);


            var claims = new[] {
                new Claim(JwtRegisteredClaimNames.Email, user.Email),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.Role, user.Role)
            };
            var token = new JwtSecurityToken(
                issuer: "Jwt:Issuer",
                audience: "Jwt:Issuer",
                claims,
                expires: DateTime.Now.AddHours(24),
                signingCredentials: credintalis);
            string encodetoken  = new JwtSecurityTokenHandler().WriteToken(token);
            string token_return = encodetoken.Substring(encodetoken.LastIndexOf('.') + 1);

            return(token_return);
        }