How to implement Azure AD Authentication in ASP.NET Core 5.0 Web Application
Tags: aspnetcoresecurity
Published: 2021-04-21 12:44:24

---
title: How to implement Azure AD Authentication in ASP.NET Core 5.0 Web Application
published: true
date: 2021-04-13 13:27:49 UTC
tags: ASPNETCoreSecurity
canonical_url: https://www.faciletechnolab.com/blog/how-to-implement-azure-ad-authentication-in-aspnet-core-50-web-application/
---

In this article, I am going to show you how to implement Azure AD Authentication in ASP.NET Core 5.0 Web application. I will create ASP.NET Core 5.0 project and show you step by step how to use it for authentication and authorization against Azure AD Authentication.

 ![how to implement Azure AD Authentication in ASP.NET Core 5.0 Web application](https://www.faciletechnolab.com/media/bq4fmn2z/azuread-cover.png "how to implement Azure AD Authentication in ASP.NET Core 5.0 Web application")

### Prerequisites

 

Before you start to follow steps given in this article, you will need an Azure Account, and Visual Studio 2019 with .NET 5.0 development environment step.

### Creating ASP.NET Core 5.0 web application

Open visual studio and click on Create a new project in the right and select “Asp.net core web app” as shown in below image and click next.

![create new project in visual studio](https://www.faciletechnolab.com/media/ktehhhtu/azure-ad-1.png?rmode=max&width=500)

In the configure your new project section enter name and location of your project as shown in below image and click next

![configure your new project in visual studio](https://www.faciletechnolab.com/media/c44bb0r2/jwt-2.png?rmode=max&width=500)

In the additional information step, select .NET 5.0 in the target framework, Authentication Type to none and check Configure HTTPS checkbox and click on create.

![configure Additional information for new project in visual studio](https://www.faciletechnolab.com/media/51tftyl0/jwt-3.png?rmode=max&width=500)

Once the project is created, run the project and copy the url of the project from the browser. We will need this url in the Azure AD app registration and setup.

 

### Setting up your App in Azure AD

Sign in to the Azure portal using an account with administrator permission. Once login, click on Azure Active Directory as shown in below image.

![](https://www.faciletechnolab.com/media/vgukjjvq/azure-ad-2.png?rmode=max&width=500)

From the left navigation, select App Registrations.

![](https://www.faciletechnolab.com/media/uhsfmu4s/azure-ad-3.png?rmode=max&height=500)

And then click on New registration from the toolbar in the top. That will open a dialog “Register and app”.

![](https://www.faciletechnolab.com/media/dg1ntack/azure-ad-4.png?rmode=max&width=500)

In the Name input field, enter the name that you want to use for the app. This name will be shown in the login page when user will be redirected to Azure Active Directory for Login.

In the supported account types, select first option. This will allow users from your own organization to login using this application.

For now, leave Redirect URI to blank and click on Register.

After the app is created, open the app by clicking on its name and copy ClientID and TenantID and keep it with you.

![](https://www.faciletechnolab.com/media/w03bniyv/azure-ad-5.png?rmode=max&width=500)

Next, click on API Permissions. Click on Add a permission from the toolbar, then click on Microsoft graph, and then delegated permissions. That will show you list of permission to select. select and add `profile` and `opendid` permissions from the list.

Next, click on Authentication from the left navigation and in the platforms section, add Web if it does not exist. In the web section, go to Redirect URL and click on Add URI in the bottom. Enter `{your-app-url}/signin-oidc`. For example, enter `https://localhost:44332/signin-oidc` if your local project is running on port 44332.

 

### Configuring ASP.NET Core 5.0 App for Azure AD Authentication

Open appsettings.json of your web application and add following lines of code.

<script src="https://gist.github.com/FacileTechnolab/fd62885b70811e60ea4bb1a68e6692e3.js"></script>

Replace `your-client-id` and `your-tenant-id` with the actual values that you copied while doing app registration in azure ad

Next, add package manager console and add following two package references to your web application.

```
Install-package Microsoft.AspNetCore.Authentication.OpenIdConnect
install-package Microsoft.Identity.Web
```

Next, open startup.cs in your project and paste following code in the `ConfigureServices` method before `services.AddRazorPages();` line.

<script src="https://gist.github.com/FacileTechnolab/74f053ef2bb9b53c1b5c34bc43c43094.js"></script>

Next, in the `Startup.cs`, go to `Configure` method and add `app.UseAuthentication();` line before `app.UseAuthorization();` line.

Next, open index.cshtml in the views folder and enter following lines of code

<script src="https://gist.github.com/FacileTechnolab/8081e4bdbb2db8e8fffea41df6bb2d56.js"></script>

Save all files and run your project.

You will notice that once you run the project, the browser will redirect to Microsoft login page and upon return, you will see your email address on the page.

 

### Conclusion

We just learned how to implement an Azure AD authentication in ASP.NET 5.0 web application.

### References

- [https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp](https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp) 
- [https://www.c-sharpcorner.com/article/authentication-and-authorization-in-asp-net-5-with-jwt-and-swagger/](https://www.c-sharpcorner.com/article/authentication-and-authorization-in-asp-net-5-with-jwt-and-swagger/)