第一步,准备工作。获取Client ID和Client Secret
1、自行登陆GitHub官网,点击Setting,如下图:
2、继续,点击Developer settings,如下图:
3、继续,点击Oauth Apps,如下图:
4、继续,点击New Oauth App,如下图:
5、继续,填写完毕后,点击Register application,如下图:
6、至此,我们已经成功拿到Client ID和Client Secret。另外,此页面还可以更改步骤5填写的相关信息,如下图:
第二步,代码部分。实现GitHub第三方登录
1、Login.aspx 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="GitHubLogin.Login" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <a href="https://github.com/login/oauth/authorize?client_id=xxxxxxxxxxxxxxxxxxxxxx&state=STATE&redirect_uri=http://www.kudsu.xyz/">github登录</a> </div> </form> </body> </html>
2、Login.aspx.cs 代码
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace GitHubLogin { public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string code = Request.QueryString["code"] == null || Request.QueryString["code"].ToString() == "" ? "" : Request.QueryString["code"].ToString(); if (code != "") { //第二步,获取token string tokenJson = LoadURLString("https://github.com/login/oauth/access_token?client_id=xxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx&code=" + code + "&redirect_uri=http://www.kudsu.xyz/", "post"); JObject jo = (JObject)JsonConvert.DeserializeObject(tokenJson); tokenJson = jo["access_token"].ToString(); //第三步,获取GitHub用户信息 string userJson = LoadURLString("https://api.github.com/user?access_token=" + tokenJson, "get"); //把GitHub用户信息输出到页面上 Response.Write(userJson); } } } /// <summary> /// 请求url /// </summary> /// <param name="url">地址</param> /// <param name="GetPost">post、get</param> /// <returns></returns> private string LoadURLString(string url, string GetPost) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url); request1.Method = GetPost; request1.ContentType = "application/json"; request1.Accept = "application/json"; request1.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); request1.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:12.0) Gecko/20100101 Firefox/12.0"; return new StreamReader(((HttpWebResponse)request1.GetResponse()).GetResponseStream(), Encoding.UTF8).ReadToEnd(); } } }
3、项目地址:https://github.com/kudsu/GitHubLogin
亲,如果可以,给个星星~