Skip to content

Instantly share code, notes, and snippets.

@germ13
Created April 10, 2018 06:41
Show Gist options
  • Save germ13/35bddfc247a2d16735d7e462af813684 to your computer and use it in GitHub Desktop.
Save germ13/35bddfc247a2d16735d7e462af813684 to your computer and use it in GitHub Desktop.
Basic Symmetric
public IEnumerable<string> Get()
{
var user = Thread.CurrentPrincipal.Identity as WindowsIdentity;
// Define const Key this should be private secret key stored in some safe place
// This is a meaningles key of course
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
// Create Security key using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
//
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
// Finally create a Token
var header = new JwtHeader(credentials);
//Some PayLoad that contain information about the customer
var payload = new JwtPayload
{
{ "greeting ", "hello "},
{ "scope", "http://winnieluk.com/"},
{ "Claims", new ArrayList{"sweet", "pretty" } },
{"sub", "me" }
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
// Token to String so you can use it ,in your client
var tokenString = handler.WriteToken(secToken);
var payloadr = handler.ReadJwtToken(tokenString);
Console.WriteLine(tokenString);
Console.WriteLine("Consume Token");
return new string[] { "value1", user.Name, tokenString,payloadr.Claims.Take(3).First().ToString(), key, payloadr.Payload.First().Value.ToString() };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment