How To Create a .Net Core 2.0 MVC App with Azure Active Directory Authentication
In this article, I will show you how easy it is to set up authentication with Azure Active Directory for an ASP.Net web app. No coding required!
First off, if you don’t have an Azure account, you can start a free trial here.
Before we start, let’s find out the domain names that we have available for our active directory users. We can find this from Azure Active Directory + Domain names
Now we can create an admin user. Go to Azure Active Directory, Users and groups + All users, and finally click on New user:
Then choose a name and a username for this user. The username needs to include the domain name that we got earlier. Set the role for this user as Global administrator, make a note of the auto-generated temporary password and click the create button.
In Visual Studio, create a new web application project, choose a name for your project and click OK.
In the next step, choose .Net Core 2.0 MVC and change the authentication to Work or School Accounts, Cloud – Single Organization, fill in your domain name as before, and check the Read directory data option.
Then you need to log on with the global account that you created on AAD. You will also be asked to change the password after the first time that you log in.
The project overview window will be shown as soon as the creation process is ready. Click on the Publish option.
Then create a new Azure App Service and click the publish button.
You will be asked to select some details for your new app. For the app service plan, I decided to host it on the free tier for development. Note that on other environments, you will need to choose the right machine for the job. However, any test service plan other than a free one will start eating into your monthly limit as soon as you create it.
The website will take a few moments to be deployed directly to Azure, but before we check that let’s try running the solution locally through Visual Studio. The familiar Microsoft AAD login is shown:
After logging in, you should see the account details displayed at the top of the web page:
Now if we want to make this work also on Azure we must do one final change. The web app that was created automatically, was also automatically configured to work with your local debugging environment. So let us fix that.
Go back to the Azure portal, then look for app registrations to choose your app:
Click settings, then Reply URLs:
Here we can see that the active directory app is only registered to accept login requests from the localhost url, so we also need to add the url of the Azure web app. You can find this url back from the overview of the app services module.
That's it. All you need to do now is to add more users to the directory.