Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JWT (JSON Web Token) Portable implementation for .NET 4.5+ (Public Domain)

License

NotificationsYou must be signed in to change notification settings

senzacionale/jwt-portable-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

Dependency

Portable.BouncyCastle, Newtonsoft.Json

Installation

The easiest way to install is via NuGet. Seehere. Else, you can download and compile it yourself.

Usage

Creating Tokens

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

Verifying and Decoding Tokens

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

exp claim

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!

Configure JSON Serialization

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp