1. 程式人生 > >第三方登入之GitHub篇

第三方登入之GitHub篇

第一步,準備工作。獲取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

    親,如果可以,給個星星~

&n