Saturday, October 16, 2021

Web App with Azure Active directory Authentication

This article shows you how to configure authentication for Web application so that your app signs in users with the Microsoft Identity Platform (Azure AD) as the authentication provider.

In this blog we will create web application in .net core with azure active directory authentication.

We need to follow below steps

1.       App registration for azure active directory authentication

2.       Once registration done then get follow information from app registration

a.       Domain (tenant.onmicrosoft.com)

b.       TenantId

c.       ClientId (Application ID)

3.       Create web application in .net core with Microsoft identity platform authentication type  

4.       Set Domain, TenantId and ClientId in your web app appsettings. json

5.       Add redirect URL for registered app  

6.       Run the application and it will authenticate with Azure Active directory

App registration for azure active directory authentication

When we want to authenticate our application with azure active directory, first we need to register app in our azure active directory. I have written in previous blog, how we can register app with help of azure portal. If you have not known about it. Please read that post. I will use same app registration (HRApp) for azure active directory authentication.

What is Azure service principal? link

In this post I have created app with name HRApp. Now I will use same app for authentication.


Create App Web in .net core

Once app is registered after that we can use that in our web application for authentication purpose. You need to follow simple steps for it.

1.       Open visual studio 2019

2.       Creating ASP.NET Core Web App with name HRApp for demo purpose


3.       Once new project name is given then click on next. It will ask for the .net core framework and authentication type. I have selected as below

 


4.       Once all selected as above click on create button. It will create web application for you.

5.       Project is created now you can open appsettings. json and some default values setted for Domain, TenantId and ClientId


6.       We need to replace these values with our app registration values like below screen


Add redirect URL in registered app 

Once all values updated, we need to get the .net web application url and add that url in app registration redirect url. In my case application URL is https://localhost:44381. I have followed below steps for it

1.       Go to azure portal https://portal.azure.com/

2.       Once login in portal go to azure active directory and select your tenant where your app was registered

3.       Go to App registrations and selected your app (HRApp)

 


4.       Click on Authentication and Add a platform for configurations. I have selected Web.


5.       Once web is selected then need to put redirect URL and checkbox checked

a.       Redirect URIs:  https://localhost:44381/signin-oidc

b.       Checked checkbox:  ID tokens (used for implicit and hybrid flows)

                 


6.       Click on configure it will show like below. You can add multiple Redirect URIs



Run the application

Now all set now, we can run our application in visual studio 2019. When we run it will do below things?

1.       Run the application and it will redirect us to Microsoft login screen 

 


2.       Enter your email id and password and click on Sign in

3.       If sign in successful it will ask for your consent


 

4.       Click on accept button. It will open web application with active directory authentication


Hope it will help you to implement active directory authentication in your web application.

Keep sharing keep learning  

6 comments: