About Jwt.net

Source: Internet
Author: User

Tags: tde dict program decryption Purpose Bubuko try pass input

Concept of 1.JWT:
The full name of the JWT is the JSON Web Token, a concise, URL-safe declarative specification for communicating security information between the two parties. JWT, as an open standard (RFC 7519), defines a concise, self-contained method for communicating information between two communication parties in the form of a JSON object. Because of the presence of digital signatures, this information is trustworthy, and JWT can be signed using the HMAC algorithm or the public-private key pair of RSA.

Structure of the 2.JWT
JWT is generally composed of three paragraphs, separated by a. Number, the first paragraph is the header, the second is payload, and the third is signature, for example:

The header (JWT's head) carries two pieces of information like JSON:
The claim type. This is JWT.
The algorithm that declares the encryption. HMAC SHA256 are usually used directly, and others are RS256, etc.

Playload load is the place where valid information is stored. The name is like the cargo on the plane, which contains three pieces of information:
Declaration of registration in the standard
Public statements
Private claims
Declarations registered in the standard (recommended but not mandatory):
ISS:JWT issued by
SUB:JWT-oriented users
AUD: The party receiving the JWT
EXP:JWT expiration time must be greater than the time of issue
NBF: Defines the time before which the JWT is not available.
IAT:JWT Time of issue
JTI:JWT's unique identity is used primarily as a one-time token to avoid replay attacks.

Public statements:
Public declarations can add any information, generally add information about the user or other necessary information for business needs. But it is not recommended to add sensitive information because that part can be decrypted on the client

Private statement:
A private statement is a statement that is defined by both the provider and the consumer, and is generally not recommended for storing sensitive information, because Base64 is symmetric, meaning that part of the information can be classified as plaintext information

Define a Playload

The third part of Signature JWT is a visa information, which consists of three parts:
Header (after Base64)
Payload (after Base64)
Secret

This section requires Base64 encrypted headers and Base64 after the encrypted payload is used. A string consisting of a connection and then a combination of encryption by adding secret by the encryption declared in the header, which then forms the third part of the JWT

How to apply
Usually add authorization in the request head and add bearer callout:

In the project's Package Manager console, enter the following:
Install-package Jwt-version 3.0.0
Installing Jwt.net 3.0.0

        static void Main (string[] args) {Idatetimeprovider Provider = new Utcdatetimeprovider (); var now = provider.            Getnow ();            var Unixepoch = new DateTime (1970, 1, 1, 0, 0, 0, DATETIMEKIND.UTC); var Secondssinceepoch = Math.Round ((Now-unixepoch).            TotalSeconds); var payload = new dictionary<string, object>{{"Name", "Mrbug"}, {"Exp", Secondssinceep            OCH+100}, {"JTI", "Luozhipeng"}};            Console.WriteLine (Secondssinceepoch);            Ijwtalgorithm algorithm = new Hmacsha256algorithm ();            Ijsonserializer serializer = new Jsonnetserializer ();            Ibase64urlencoder Urlencoder = new Jwtbase64urlencoder ();            Ijwtencoder encoder = new Jwtencoder (algorithm, serializer, urlencoder); String secret = "123";//Key var token = encoder.            Encode (payload, secret);            Console.WriteLine (token); Decrypt (TOKEN,SECRET);        Console.readkey (); }///<summary>//Decrypt///</summary>//<param name= "token" >token information </p aram>//<param name= "secret" > Keys </param> private static void Decrypt (String token,string sec                RET) {try {Ijsonserializer serializer = new Jsonnetserializer ();                Idatetimeprovider Provider = new Utcdatetimeprovider ();                Ijwtvalidator validator = new Jwtvalidator (serializer, provider);                Ibase64urlencoder Urlencoder = new Jwtbase64urlencoder ();                Ijwtdecoder decoder = new Jwtdecoder (serializer, validator, urlencoder); var json = decoder.            Decode (token, secret, verify:true);//token for the previously generated string Console.WriteLine (JSON);            } catch (Tokenexpiredexception) {Console.WriteLine ("Token has expired"); } catch (SignaturevErificationexception) {Console.WriteLine ("Token has invalid signature"); }        }

About Jwt.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: