. Net Oauth2.0 third-party login Development (Facebook, LinkedIn), oauth2.0facebook

Source: Internet
Author: User

. Net Oauth2.0 third-party login Development (Facebook, LinkedIn), oauth2.0facebook

Requirement: oau2implements Third-Party website authorization and obtains relevant data for login and other functions

Currently, Facebook and LinkedIn are supported, which are basically the same, but the returned data is different and must be processed based on the specific response type.

 

1. oau2authentication Process

The oau2authentication protocol involves three Parties (applications, users, and service providers). In addition, the process is cumbersome and the implementation names are different,

The authentication process is as follows:

1. Apply for an application from the service provider using oau2's authentication, and obtain the client_id (unique application ID) and client_secret (application private key) of the application)

2. Use the key/secret to request the user to authorize the Token (code is authorization_code) to the service provider)

3. Use the user-authorized Token in exchange for the user information to access the Token (access_token ),

4. Use access_token (user information access token) to obtain relevant information

 

2. Authorization Access Process

1. Obtain the access permission (client_id and client_secret) from a third-party platform)

2. Enter the link returned from the Oauth2.0 website

3. Send authorization requests to third-party platforms

4. Return to the url for business tide Processing

Note: The requested url must be consistent with the actual url.

 

3. AuthHelper code

Public abstract class AuthHelper {public static AuthToken GetToken (string code, string token_url, string cliend_id, string client_secret, string return_url) {var strResult = GetTokenStr (code, token_url, cliend_id, Token, return_url); try {var res = JsonConvert. deserializeObject <AuthToken> (strResult); return res;} catch (Exception ex) {Tool. log. write (ex. toString ();} return default (AuthToken );} /// <summary> /// send a token request to a third-party platform /// </summary> /// <param name = "code"> </param> // /<param name = "token_url"> </param> // <param name = "cliend_id"> </param> // <param name = "client_secret"> </param> /// <param name = "return_url"> </param> /// <returns> </returns> public static string GetTokenStr (string code, string token_url, string cliend_id, string client_secret, string return_url) {Dictionary <string, string> dicPara = new Dictionary <string, string> (); dicPara. add ("grant_type", "authorization_code"); dicPara. add ("code", code); dicPara. add ("redirect_uri", return_url); dicPara. add ("client_id", cliend_id); dicPara. add ("client_secret", client_secret); var token = WebApiHelper. postResponseStr (token_url, dicPara); return token ;} /// <summary> /// send the token in the header /// </summary> /// <param name = "accessToken"> </param> /// <param name = "profile_url"> </param> // <returns> </returns> public static string GetProFileAuth (string accessToken, string profile_url) {Dictionary <string, string> dicAuth = new Dictionary <string, string> (); dicAuth. add ("Authorization", "Bearer" + accessToken); var profile = WebApiHelper. getResponseStr (profile_url, null, dicAuth); return profile ;} /// <summary> /// get token // </summary> /// <param name = "accessToken"> </param> /// <param name = "profile_url"> </param> // <returns> </returns> public static string GetProFileStr (string accessToken, string profile_url) {Dictionary <string, string> dicQuery = new Dictionary <string, string> (); dicQuery. add ("access_token", accessToken); var profile = WebApiHelper. getResponseStr (profile_url, dicQuery, null); return profile ;}}

4. Return to business processing

Public ActionResult ReturnLinkedin () {string description = string. empty; string code = RequestString ("code"); string state = RequestString ("state"); string error = RequestString ("error "); string error_description = RequestString ("error_description"); if (code = "" | error! = "") {If (code = "user_cancelled_authorize" | code = "user_cancelled_login") {description = code;} else description = error! = ""? Error_description: "no authentication! ";} Else {var res = Tools. Auth. LinkinHelper. GetToken (code, Tools. Auth. LinkinConfig. ReturnUrl); if (res. access_token! = "") {Var entity = Tools. Auth. LinkinHelper. GetProFileStr (res. access_token, Tools. Auth. LinkinConfig. ProfileResourceUrl); description = entity;
/*** Specific Service Processing
**/} Else {description = "access token error" ;}} ViewBag. Description = description; return View ();}

Github address: https://github.com/willianchen/Chml.Oauth

Post a blog for the first time. If you have any questions or suggestions, please leave a message.

 

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.