Skip to content

Instantly share code, notes, and snippets.

@WernerMairl
Created March 2, 2019 15:54
Show Gist options
  • Save WernerMairl/45689918aba0953c86d0869d0a4043f1 to your computer and use it in GitHub Desktop.
Save WernerMairl/45689918aba0953c86d0869d0a4043f1 to your computer and use it in GitHub Desktop.
Create Jwt Bearer Token (sample)
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes("mysecret12345abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN");
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, "myuserid")
}),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
string tk = tokenHandler.WriteToken(token);
Console.WriteLine(string.Format("Token: {0}", tk));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment