Encryption and decryption of ASP. Oauth:access token, client secret and Refresh token generation

Source: Internet
Author: User
Tags oauth

in ASP. OWIN OAuth (Microsoft.Owin.Security.OAuth), the default encryption method for access token is:

1) System.Security.Cryptography.DpapiDataProtector.Protect () 2) convert.tobase64string () 3). TrimEnd ('='). Replace ('+'-'). Replace ('/'_');

The default decryption method for access tokens is:

1) System.Security.Cryptography.DpapiDataProtector.Unprotect ()2) Pad (text. Replace ('-'+'). Replace ('_'/')); 3) convert.frombase64string ()

The implementation code of the Pad method is as follows:

Private Static stringPad (stringtext) {    varpadding =3-(text. Length +3) %4); if(padding = =0)    {        returntext; }    returnText +New string('=', padding);}

For client secret and refresh token generation, OWIN OAuth does not provide a ready-made method, we use RNGCryptoServiceProvider, the code is as follows:

RandomNumberGenerator Cryptorandomdatagenerator =NewRNGCryptoServiceProvider ();byte[] buffer =New byte[ the];cryptorandomdatagenerator.getbytes (buffer);stringSecretortoken =convert.tobase64string (buffer). TrimEnd ('='). Replace ('+','-'). Replace ('/','_');

Resources

Katana Source Code

How to generate OAuth 2 Client Id and Secret

Encryption and decryption of ASP. Oauth:access token, client secret and Refresh token generation

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.