[learning notes] GitHub chapter of third party login

Keywords: ASP.NET github JSON Windows Firefox

The first step is preparation. Get Client ID and Client Secret

1. Log in GitHub official website and click Setting, as shown below:

2. Continue, click Developer settings, as shown below:

3. Continue, click Oauth Apps, as shown below:

4. Continue, click new origin app, as shown below:

5. Continue. After filling in, click Register application, as shown below:

6. So far, we have successfully obtained the Client ID and Client Secret. In addition, the information filled in in step 5 can be changed on this page, as shown below:

 

The second step is the code part. Implementation of GitHub third party login

1. Login.aspx code

<%@ 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 Sign in</a>
        </div>
    </form>
</body>
</html>

2. Login.aspx.cs code

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 != "")
                {
                    //Step 2, get 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();
                    //Step 3, get GitHub User information
                    string userJson = LoadURLString("https://api.github.com/user?access_token=" + tokenJson, "get");
                    //hold GitHub User information output to page
                    Response.Write(userJson);
                }
            }
        }
        /// <summary>
        /// request url
        /// </summary>
        /// <param name="url">address</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. Project address: https://github.com/kudsu/GitHubLogin

Give a star if you can~

Posted by Waseem Ullah Kh on Thu, 17 Oct 2019 07:35:37 -0700