[Original] Old repeat: only configure parameters to achieve oau2login, old repeat oau22.

Source: Internet
Author: User
Tags oauth

[Original] Old repeat: only configure parameters to achieve oau2login, old repeat oau22.

In fact, this component has been written for a long time. For a few months, it has been put on the MrHuo studio and has always been said to be organized. It is too busy to have time.

In addition, for some basic content about oau2, please look for information from the Internet, too much, cumbersome to write.

Not to mention nonsense. First, it is not easy.

The project uses MVC5. In fact, WebForm can also be used. Just do the frontend portal and callback method.

Configuration file:

I have sorted out these configurations for the moment, and others want to search for official document configurations by themselves.

 

Take a look at the content (read the content and don't scold me, I admit it's a bit of a title party ):

In fact, the principle is to sort out some oau2's rules. The oau2interfaces on each platform are basically the same, but slightly different. You can obtain AuthorizationCode by configuring some parameters, the next step is to get user information.

With these configuration files, you have to include a DLL file written by me.

----------------------------------------- I'm a cut-off rule ----------------------------------------------

Check the front-end code:

View of the OAuth logon entry:

@ Using MrHuo. OAuthLoginLibs. core; @ {ViewBag. title = "social login component" ;}< h2> MrHuo studio social login component 


OAuthController has Index Action, whose content is return View ();

The code used to log on to the Controller Using OAuth:

Using System; using System. collections. generic; using System. linq; using System. text. regularExpressions; using System. web; using System. web. mvc; using Codeplex. data; using MrHuo. OAuthLoginLibs; using MrHuo. OAuthLoginLibs. core; using MrHuo. OAuthLogin. QQApis; namespace TestOAuth. controllers {public class SocialController: Controller {public SocialController () {} OAuthLogin oauthLogin = new OAuthLogin (); pub Lic ActionResult OAuth (string platform) {return getPlatformActionResult (platform);} public ActionResult LoginCallback (string code, string state) {try {if (string. isNullOrWhiteSpace (code) {return View ("Error", (object) ("Logon Error:" + Request. queryString. toString () + "<br/> Url:" + Request. url);} string ret = string. empty; var result = oauthLogin. login (code, state); if (result. error! = Null) {return View ("Error", (object) result. error. message);} if ("QQ ". isFullEqual (result. config. platform) {var qqContext = new QQContext (result. config, result. serverResponse); var user = qqContext. getUserInfo (); ret + = user. nickName + ", ," + user. gender + "<br/>";} ret + = "Platform" + result. config. platform + "Logined Result: <br/>" + result. serverResponse; return View (object) ret);} catch (Exception ex) {return View ("Error", (object) ex. message) ;}} private ActionResult getPlatformActionResult (string platform) {try {oauthLogin. beginAuthoration (platform);} catch (Exception ex) {return View ("Error", (object) ex. message) ;}return null ;}}}


Code explanation:

Public ActionResult OAuth (string platform)

This method is purely a unified logon portal that is passed into the oau2authenticate platform.

 

Public ActionResult LoginCallback (string code, string state)

This Action is written in the callback address of other platforms during OAuth authentication. The code and state parameters are the values passed by other platforms after the OAuth login is complete.

Code is AuthorizationCode, which is an important credential used to exchange AccessToken.

 

Configuration in RouteConfig:

routes.MapRoute(                name: "SocialDefault",                url: "Social/OAuth/{platform}",                defaults: new { controller = "Social", action = "OAuth" }            );


It's so simple, a lot of writing, in fact, the actual operation is very simple.

----------------------------------------- I'm a cut-off rule ----------------------------------------------

Careful students may have discovered that I wrote the code for obtaining QQ user information in LoginCallback Action. Yes, I wrote a test, and others had time to write it again.

I will open the source code, and I need to add QQ to my current code. I am afraid that people will laugh at me after the code is put out. Alas, people with poor technology are always not confident.

I will put it on github. I hope everyone can complete this component. In this way, if I get down one day, you can also use my components. I am very happy ....

 

Conclusion:

Tanabata passed, as always, quietly passed, and the door did not go out .....

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.