其實這個組件寫出來很長時間了,有幾個月吧,一直在 MrHuo工作室 上放著,一直說要整理,太忙沒時間。
另外,關於OAuth2的一些基礎內容還請從網上找找資料,太多了,寫的累贅。
廢話不多說,先上圖,無圖不快。
項目采用MVC5,其實WebForm也可以,做一下前台入口和回調方法就可以了。
配置文件:

我暫時整理了這麼些配置,其他想要的自己去搜索官方文檔配置。
大概看一下內容(看了內容別罵我,我承認有點標題黨):

其實原理就是整理了OAuth2的一些規則,各個平台的OAuth2接口基本一致,但略有不同,配置一些參數就可以獲取到AuthorizationCode,接下來獲取用戶信息就是很簡單的事情了。
那麼有了這些配置文件,還得有我寫的一個DLL文件,引入進去就可以了。
-------------------------------------------I‘am a cut-off rule----------------------------------------------
看下前台的代碼:
顯示OAuth登錄入口的View:
@using MrHuo.OAuthLoginLibs.Core;
@{
ViewBag.Title = "社會化登錄組件";
}
<h2>MrHuo工作室社會化登錄組件</h2>
@{
var platforms = AuthConfigManager.GetAllLoginConfigs().OrderBy(p => p.DisplayIndex);
foreach (var config in platforms)
{
<input type="button" class="btn btn-default" value="@(config.Platform)登錄" onclick="location.href='/Social/OAuth/@config.Platform'" @(!config.Enabled ? "disabled='disabled' title='未啟用“" + config.Platform + "”登錄'" : "") />
}
}
OAuthController裡有Index Action,內容為return View();
OAuth請求登陸Controller裡的代碼:
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();
public ActionResult OAuth(string platform)
{
return getPlatformActionResult(platform);
}
public ActionResult LoginCallback(string code, string state)
{
try
{
if (string.IsNullOrWhiteSpace(code))
{
return View("Error", (object)("登錄發生錯誤:" + 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 + ",<img src='" + user.Avatar + "' />," + user.Gender + "<br /><br />";
}
ret += "Platform " + result.Config.Platform + " Logined Result: <br /><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;
}
}
}
代碼解釋:
public ActionResult OAuth(string platform)
這個方法純粹就是個統一登錄入口,傳入OAuth2認證的平台。
public ActionResult LoginCallback(string code, string state)
這個Action是填寫在OAuth認證時填寫在其他平台的回調地址。其中的code和state參數是OAuth登錄完畢後,其他平台傳過來的值。
code是AuthorizationCode,是用來換取AccessToken的重要憑據。
RouteConfig裡配置:
routes.MapRoute(
name: "SocialDefault",
url: "Social/OAuth/{platform}",
defaults: new { controller = "Social", action = "OAuth" }
);
就這麼簡單,寫的很多,其實實際操作起來,很簡單。
-------------------------------------------I‘am a cut-off rule----------------------------------------------
細心的同學可能發現了,我在LoginCallback Action裡寫了獲取QQ用戶信息的代碼,是的,我就寫了一個測試的,其他的有時間再寫。
代碼我會開源的,需要現在代碼的同學加我QQ,跟我要把,我怕代碼放出去後別人笑我,唉,技術不咋的人總是不自信。
我會放到github上,希望大家都來完善這個組件。這樣要是我有一天掛了,大家還可以用到我的組件。我很開心。。。。
結束語:
七夕過了,一如既往,悄悄的過了,門都沒出。。。。。