其实这个组件写出来很长时间了,有几个月吧,一直在 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上,希望大家都来完善这个组件。这样要是我有一天挂了,大家还可以用到我的组件。我很开心。。。。
结束语:
七夕过了,一如既往,悄悄的过了,门都没出。。。。。