Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
5
Indexable
 var claims = new[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, sub),
            new Claim(JwtRegisteredClaimNames.Jti, jti),
            new Claim(JwtRegisteredClaimNames.Iat, iat.ToString(), ClaimValueTypes.Integer64),
        };

        // Create symmetric security key
        var key = new SymmetricSecurityKey(Convert.FromBase64String(secret));

        // Create signing credentials using the key and algorithm
        var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

        // Create token descriptor
        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Issuer = issuer,
            Audience = audience,
            Subject = new ClaimsIdentity(claims),
            Expires = DateTime.UtcNow.AddHours(1), // Adjust the expiration as needed
            SigningCredentials = signingCredentials,
        };

        // Create token handler
        var tokenHandler = new JwtSecurityTokenHandler();

        // Generate token
        var token = tokenHandler.CreateToken(tokenDescriptor);

        // Write the token as a string
        var tokenString = tokenHandler.WriteToken(token);

        return tokenString;
Editor is loading...
Leave a Comment