Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET MVC
  4. Login With Github in Asp.net MVC

Login With Github in Asp.net MVC

Date- May 06,2023 Updated Feb 2026 8071 Free Download Pay & Download
Login With Github AspNet MVC

Hello guys, welcome to Code2Night! In this blog post, we will be Integrating GitHub login in an ASP.NET MVC application. We will go through step-by-step instructions on how to set up the GitHub OAuth application, configure our ASP.NET MVC application to handle Github authentication, and finally, test the authentication flow.

As web developers, we often need to implement social login in our web applications to provide a convenient authentication mechanism for our users. Among the popular social logins, GitHub login has gained a lot of popularity in recent years.

So, if you are looking to implement GitHub login in your ASP.NET MVC application, keep reading and let's get started!

GitHub Authentication

To allow users to log in to your C# application using their GitHub credentials, you can use the OAuth 2.0 authentication flow and the GitHub API. Here's how to implement GitHub login in C#

So the first step is always to create one GitHub client id and client secret to use the authentication. So we have to follow the steps mentioned below

Step 1- Login into your Github Account

Create a new OAuth application in your GitHub account by going to your account settings, selecting "Developer settings" from the sidebar, and then "OAuth Apps". Click the "New OAuth App" button and fill out the form with your application's details. Make sure to specify a callback URL that your application will redirect to after authentication. Check below screenshots

After Login into your account, you have to go to settings and you will see this screen. Here click on Developer Settings as highlighted

Integrating GitHub Login

Step 2- Go to Oauth Apps

In the developer settings, click on "OAuth Apps" like in the screenshot below

Login With Github in Aspnet MVCLogin With Github in Aspnet MVC 2

Step 3- Register a New Application

We have to register a new application to obtain the client id and client secret. You can fill in the details in the following manner and make sure you have the correct callback URL. Now click on Register a new applicationLogin With Github in Aspnet MVC 3

Step 4- Generate the Client's Secret

Now the most important part is to generate the client's secret. So you have to click on the button shown in the screenshot below and then copy the client id and client secret

Login With Github in Aspnet MVC 4


Login With Github in Aspnet MVC 5

Now we have the client id and client secret from the step above. Now we can go to our Asp.net application and first of all, we have to install the Octokit Nuget package. 

Login With Github in Aspnet MVC 6

After you have installed this package, go to your application controller and add the following namespace


using Octokit;

Now we have to go to the web. config and add the following keys there

<add key="GithubClientId" value="f1970dff4567087b253" />
	<add key="GithubClientSecret" value="b2df2efe65e677116123345b3f589456343163f1" />
	<add key="RedirectUri" value="https://localhost:44320/Home/GithubLogin" />

Now we have to go to the action and get these keys from web config and pass those to view bag to go to the view

 public async Task<ActionResult> Index()
        {
            ViewBag.ClientId = ConfigurationManager.AppSettings["GithubClientId"].ToString();
            ViewBag.RedirectUrl = ConfigurationManager.AppSettings["RedirectUri"].ToString();
            return View();
        }

Now we have to go to the view and add an anchor tag that will act like login with GitHub button you have to add the following in the view

<div class="jumbotron">
    <h1>ASP.NET Github Login</h1>
   
    <p><a href="https://github.com/login/oauth/authorize?client_id=@ViewBag.ClientId&redirect_uri=@ViewBag.RedirectUrl&scope=user:email" class="btn btn-primary btn-lg">Github Login</a></p>
</div>

Now that we have done adding the button when you will click on this it will redirect to GitHub for authentication, and after authentication, it will return back the authorization code on the redirect URI that you have mentioned on the button. So we have to create the following action for handling callback


public async Task<ActionResult> GithubLogin(string code)
        {
            var client = new HttpClient();
            var parameters = new Dictionary<string, string>
            {
            { "client_id", ConfigurationManager.AppSettings["GithubClientId"].ToString() },
            { "client_secret", ConfigurationManager.AppSettings["GithubClientSecret"].ToString()},
            { "code", code },
            { "redirect_uri", ConfigurationManager.AppSettings["RedirectUri"].ToString()}
            };
            var content = new FormUrlEncodedContent(parameters);
            var response = await client.PostAsync("https://github.com/login/oauth/access_token", content);
            var responseContent = await response.Content.ReadAsStringAsync();
            var values = HttpUtility.ParseQueryString(responseContent);
            var access_token = values["access_token"];
            var client1 = new GitHubClient(new ProductHeaderValue("Code2night"));
            var tokenAuth = new Credentials(access_token);
            client1.Credentials = tokenAuth;
            var user = await client1.User.Current();
            var email = user.Email;
            return View(user);
        }

Now, in the above action, we are getting the code from the callback and using that to generate an access token and then we using Octokit to use that token and get the user details. Now you can add one new view with the name GithubLogin.cshtml and add the following code

@model Octokit.User
@{
    ViewBag.Title = "Contact";
}
<h2>@(Model.Login)</h2>


Now run the application and you will see the following screen

Login With Github in Aspnet MVC 7Now click on GitHub login button and it will redirect to another screen where you have to add username and password and after correct credentials, you will see the following screen

Login With Github in Aspnet MVC 8

After the user authorizes your application, GitHub will redirect them back to your callback URL with an authorization code.

This code sends a POST request to GitHub's access token endpoint to exchange the authorization code for an access token. The access token can then be used to make requests to the GitHub API on behalf of the user.

Login With Github in Aspnet MVC 9

and in the user variable you will be able to see the user details

Login With Github in Aspnet MVC 10

So, this is how we can implement Github Authentication or Login With Github in our Asp.Net MVC project.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Translating Website Data Using Azure Translator API in ASP.NET MVC
Feb 01, 2025
How to Convert Text to Speech in Asp.Net
Nov 06, 2023
How to export table data in pdf using itextsharp in asp.net mvc
Sep 16, 2023
How to generate pdf using itextsharp in asp.net mvc
Aug 06, 2023
Previous in ASP.NET MVC
Implement Facebook Login in Asp.Net MVC
Next in ASP.NET MVC
Download Files as Zip file in Asp.Net
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,933 views
  • 2
    Error-An error occurred while processing your request in .… 11,269 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 233 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,455 views
  • 5
    Mastering JavaScript Error Handling with Try, Catch, and F… 160 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,491 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,225 views

On this page

🎯

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Implement Stripe Payment Gateway In ASP.NET 58741 views
  • Jquery Full Calender Integrated With ASP.NET 39653 views
  • Microsoft Outlook Add Appointment and Get Appointment using … 27579 views
  • How to implement JWT Token Authentication and Validate JWT T… 25283 views
  • Payumoney Integration With Asp.Net MVC 23225 views
View all ASP.NET MVC posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor