Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Login With Microsoft in Asp.net

Login With Microsoft in Asp.net

Date- May 14,2023

8439

Free Download Pay & Download
Microsoft Authentication Login With Microsoft

Microsoft Authentication

Hello everyone and welcome to Code2Night! In today's digital era, incorporating social logins into our web applications has become increasingly important. It provides users with a seamless and convenient way to access various platforms without the hassle of creating new accounts. In this article, we will dive into the world of .net and explore how to implement the "Login With Microsoft" feature. So, grab your coding hats, and let's get started on this exciting journey of integrating Microsoft login functionality into our asp.net applications!

Login with MS is used nowadays for login in web applications. For implementing this we will need to create a new project and API credentials in Microsoft Azure Portal. So you can check in the next steps for implementing MS Authentication

First of all, we have to log in to Azure Portal. You have to search for the App Registration option in the search barMicrosoft

After this, we have to click on App registration which will lead to a page where you will create a new application as shown in the next image

Clicking on this new registration button will redirect you to a new page where you have to add your application details and redirect URLs

After clicking on register you will redirect to a new screen where you will be able to see client id and application details. You will see the screen shown in the screenshot below

You have to click on the highlighted link above for creating a client secret key for the authentication.


After this step, you will be able to get your client's secret key as shown in the image below

You have to copy the client value and paste it somewhere as we will use this in the code. Now go back to the main screen using the Overview menu

Now you have to click on the option highlighted in the image below

Now, on the below screen, you have to check the following checkboxes and save the page details.


Now just copy the client id from the overview screen as shown below

Since now we have both client id and client secret we can move to use that in the code.

So now install the following RestSharp package in your application.

Now, you have to go the action, it must be with the same name which you mentioned in the redirect URL while getting the client. We have to use the following code

  public IActionResult Index(string code)
        {
            if (!string.IsNullOrWhiteSpace(code))
            {
                var client = new RestClient("https://login.microsoftonline.com/common/oauth2/v2.0/token");
                var request = new RestRequest(Method.POST);
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                request.AddParameter("grant_type", "authorization_code");
                request.AddParameter("code", code);
                request.AddParameter("redirect_uri", "https://localhost:44322/Home/Index");
               
                request.AddParameter("client_id", "4c073447-d7ec-4c45-b711-3d9132a9b490");
                request.AddParameter("client_secret", "jW58Q~fPR6Tt77IzapcdF2PwsalFVc_8vrConcMQ");
                
                IRestResponse response = client.Execute(request);
                var content = response.Content;
                var res = (JObject)JsonConvert.DeserializeObject(content);
                var client2 = new RestClient("https://graph.microsoft.com/v1.0/me");
                client2.AddDefaultHeader("Authorization", "Bearer " + res["access_token"]);
                request = new RestRequest(Method.GET);
                var response2 = client2.Execute(request);
              
                var content2 = response2.Content;
                
                var useremail = (JObject)JsonConvert.DeserializeObject(content2);
            }
            return View();
        }

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<a href="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=4c077747-d7ec-4c75-b72b-3d91a259a490&response_type=code&redirect_uri=https://localhost:44322/Home/Index&response_mode=query&scope=offline_access%20user.read%20mail.read&state=12345">Login With Microsoft</a>
Add the following code to your page. Remember to replace the client id and The Redirect Url mentioned in the href as per your project.



Replace this client id and redirect URL, with your created credentials


Now you can run the project and see you will see Login with Microsoft button, now on clicking that it will go to the Google sign-in screen


This is what you will get after clicking on the button, you can Add a new Google account or use an existing one. On the selection, it will go to your callback method.

You will get the logged-in user details like this in the callback method.


You can get the complete code by downloading the attachment. Let us know if you face any issues.

This is how we can implement Login With Microsoft in .Net.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

How to refund payment using Paypal in Asp.Net MVC
Jan 30, 2024
How to Use Stored Procedures with Parameters in Dapper
Jan 23, 2024
Repopulating and Reselecting same Row in DevExpress Grid
Jan 19, 2024
Get random number in asp.net C#
Dec 23, 2023
Previous in ASP.NET Core
Get Channel Comments using YouTube Data Api in Asp.Net
Next in ASP.NET Core
Get Mime Type for any extension in Asp.Net

Comments

Contents

๐ŸŽฏ

Interview Prep

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

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 25935 views
  • Exception Handling Asp.Net Core 20718 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20195 views
  • How to implement Paypal in Asp.Net Core 19606 views
  • Task Scheduler in Asp.Net core 17495 views
View all ASP.NET Core 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 | 1760
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
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • 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