[Original]: Only configuration parameters to implement OAUTH2 login

Source: Internet
Author: User
Tags oauth

In fact, this component has been written for a long time, a few months, has been in the Mrhuo studio, has been said to tidy up, too busy no time.

In addition, about OAuth2 some of the basic content also please look for information from the Internet, too much, write the cumbersome.

Don't say much nonsense, first, no pictures unpleasant.

Project adopt MVC5, actually WebForm also can, do a front entrance and callback method on can.

Configuration file:

I've been sorting through the configuration for the time being, and others want to search the official document configuration.

Probably look at the content (read the content do not scold me, I admit a bit of the title Party):

In fact, the principle is to collate some of the OAuth2 rules, the OAuth2 interface of each platform is basically the same, but slightly different, configuration of some parameters can be obtained to Authorizationcode, the next access to user information is very simple thing.

Then with these configuration files, I have to write a DLL file, introduced in it.

-------------------------------------------I ' am a cut-off rule----------------------------------------------

Look at the code of the foreground:

Show the view of the OAuth login Portal:

@using mrhuo.oauthloginlibs.core;@{viewbag.title="Social Login Components";}@{    varplatforms = Authconfigmanager.getallloginconfigs (). (P =P.displayindex); foreach(varConfiginchplatforms) {        <input type="Button" class="btn Btn-default"Value="@ (config. Platform) Login"onclick="location.href= '/social/oauth/@config. Platform '"@ (!config. Enabled?"disabled= ' disabled ' title= ' not enabled '"+ CONFIG. Platform +""Login"":"")/>    }}


Oauthcontroller has the index Action, the content is return View ();

OAuth requests the code that is logged into the controller:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text.RegularExpressions;usingsystem.web;usingSYSTEM.WEB.MVC;usingCodeplex.data;usingmrhuo.oauthloginlibs;usingMrHuo.OAuthLoginLibs.Core;usingMrHuo.OAuthLogin.QQApis;namespacetestoauth.controllers{ Public classSocialcontroller:controller { PublicSocialcontroller () {} Oauthlogin Oauthlogin=NewOauthlogin ();  PublicActionResult OAuth (stringplatform) {            returnGetplatformactionresult (platform); }         PublicActionResult Logincallback (stringCodestringState ) {            Try            {                if(string. Isnullorwhitespace (code)) {returnView ("Error", (Object)("The login error occurred:"+ Request.QueryString.ToString () +"<br/>url:"+request.url)); }                stringRET =string.                Empty; varresult =Oauthlogin.login (Code, State); if(result.) Error! =NULL)                {                    returnView ("Error", (Object) result.                Error.message); }                if("QQ". Isfullequal (result. Config.platform)) {varQqcontext =NewQqcontext (result. Config, result.                    Serverresponse); varuser =Qqcontext.getuserinfo (); RET+ = user. Nickname +", "+ user. Avatar +"'/>"+ user. Gender +"<br/><br/>"; } ret+="Platform"+ result. Config.platform +"logined Result: <br/><br/>"+result.                Serverresponse; returnView (Object) ret); }            Catch(Exception ex) {returnView ("Error", (Object) ex.            Message); }        }        PrivateActionResult Getplatformactionresult (stringplatform) {            Try{oauthlogin.beginauthoration (platform); }            Catch(Exception ex) {returnView ("Error", (Object) ex.            Message); }            return NULL; }    }}


Code Explanation:

Public ActionResult OAuth (string platform)

This method is purely a unified login portal, the introduction of OAUTH2 authentication platform.

Public ActionResult Logincallback (string code, string state)

This action is to fill in the callback address of the other platform when the OAuth authentication is completed. The code and state parameters are the values that are passed by other platforms after OAuth is signed in.

Code is Authorizationcode, which is used in exchange for accesstoken important credentials.

Configuration in Routeconfig:

routes. MapRoute (                "socialdefault",                "social/oauth/{ Platform}",                new"social"" OAuth "  }            );


Just so simple, write a lot, in fact, actually operation, very simple.

-------------------------------------------I ' am a cut-off rule----------------------------------------------

Careful classmate may find, I wrote in logincallback action to get QQ user information code, yes, I wrote a test, and other time to write.

Code I will open source, need to now code classmate add me QQ, and I want to put, I am afraid of code out to others laugh after me, alas, technology is not a person always not confident.

I'm going to put it on GitHub and I want everyone to refine this component. So if I hang up one day, you can still use my component. I am very happy....

Conclusion:

Tanabata past, as always, quietly past, the door is not out ...

[Original]: Only configuration parameters to implement OAUTH2 login

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.