Created
March 2, 2019 15:54
-
-
Save WernerMairl/45689918aba0953c86d0869d0a4043f1 to your computer and use it in GitHub Desktop.
Create Jwt Bearer Token (sample)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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