Skip to content

Instantly share code, notes, and snippets.

@lenew
Created July 26, 2017 17:02
Show Gist options
  • Save lenew/56825949ca2bc8c4cc35a32a55805947 to your computer and use it in GitHub Desktop.
Save lenew/56825949ca2bc8c4cc35a32a55805947 to your computer and use it in GitHub Desktop.
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
namespace JwtTest
{
class Program
{
static void Main(string[] args)
{
var keyBytes = new byte[64];
var passwordBytes = Encoding.UTF8.GetBytes("security");
Buffer.BlockCopy(passwordBytes, 0, keyBytes, 0, passwordBytes.Length);
SymmetricSecurityKey key = new SymmetricSecurityKey(keyBytes);
JwtHeader header = new JwtHeader(new Microsoft.IdentityModel.Tokens.SigningCredentials(key, "HS256"));
JwtPayload payload = new JwtPayload();
payload.Add("sub", "1234567890");
payload.Add("name", "John Doe");
payload.Add("admin", true);
JwtSecurityToken token = new JwtSecurityToken(header, payload);
JwtSecurityTokenHandler h = new JwtSecurityTokenHandler();
Console.WriteLine(h.WriteToken(token));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment