Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
1
Indexable
Never
 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;
Leave a Comment