- Notifications
You must be signed in to change notification settings - Fork5
JWT (JSON Web Token) Portable implementation for .NET 4.5+ (Public Domain)
License
senzacionale/jwt-portable-dotnet
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This library is Portable copy ofhttps://github.com/jwt-dotnet/jwt and supports generating and decodingJSON Web Tokens.
Supported algorithms: HMAC signatures with HS256, HS384 and HS512.
Portable.BouncyCastle, Newtonsoft.Json
The easiest way to install is via NuGet. Seehere. Else, you can download and compile it yourself.
varpayload=newDictionary<string,object>(){{"claim1",0},{"claim2","claim2-value"}};varsecretKey="GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";stringtoken=JWT.JsonWebToken.Encode(payload,secretKey,JWT.JwtHashAlgorithm.HS256);Console.WriteLine(token);
Output will be:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGFpbTEiOjAsImNsYWltMiI6ImNsYWltMi12YWx1ZSJ9.8pwBI_HtXqI3UgQHQ_rDRnSQRxFL1SR8fbQoS-5kM5s
vartoken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGFpbTEiOjAsImNsYWltMiI6ImNsYWltMi12YWx1ZSJ9.8pwBI_HtXqI3UgQHQ_rDRnSQRxFL1SR8fbQoS-5kM5s";varsecretKey="GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";try{stringjsonPayload=JWT.JsonWebToken.Decode(token,secretKey);Console.WriteLine(jsonPayload);}catch(JWT.SignatureVerificationException){Console.WriteLine("Invalid token!");}
Output will be:
{"claim1":0,"claim2":"claim2-value"}
You can also deserialize the JSON payload directly to a .Net object with DecodeToObject:
varpayload=JWT.JsonWebToken.DecodeToObject(token,secretKey)asIDictionary<string,object>;Console.WriteLine(payload["claim2"]);
which will output:
claim2-value
As described in theJWT RFC theexp
"claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing." If anexp
claim is present and is prior to the current time the token will fail verification. The exp (expiry) value must be specified as the number of seconds since 1/1/1970 UTC.
varunixEpoch=newDateTime(1970,1,1,0,0,0,DateTimeKind.Utc);varnow=Math.Round((DateTime.UtcNow-unixEpoch).TotalSeconds);varpayload=newDictionary<string,object>(){{"exp",now}};varsecretKey="GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";stringtoken=JWT.JsonWebToken.Encode(payload,secretKey,JWT.JwtHashAlgorithm.HS256);stringjsonPayload=JWT.JsonWebToken.Decode(token,secretKey);// JWT.SignatureVerificationException!
By default JSON Serialization is done by System.Web.Script.Serialization.JavaScriptSerializer. To configure a different one first implement the IJsonSerializer interface.
publicclassCustomJsonSerializer:IJsonSerializer{publicstringSerialize(objectobj){// Implement using favorite JSON Serializer}publicTDeserialize<T>(stringjson){// Implement using favorite JSON Serializer}}
Next configure this serializer as the JsonSerializer.
JsonWebToken.JsonSerializer=newCustomJsonSerializer();
About
JWT (JSON Web Token) Portable implementation for .NET 4.5+ (Public Domain)
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.