Visual Studio 2019
- Select File | New Project
- Choose ASP.NET Core Web API
- For Framework, select .NET 5
- For Authentication Type, choose Microsoft identity platform
- The connected services window appears, and starts the Microsoft identity platform configuration. Click Finish in the Required components dialogue.
- If this is the first time you use this experience, click Configure on Microsoft identity platform in the connected services page. If this is not the first time, the page will open automatically. You'll see the list of tenants where you are a user. Choose one from the list.
- Click the Green plus sign to create a new app in the selected tenant. Give it a name, and Click Register. The app will be created in Azure AD.
- Click Next. Then Finish.
Run the application from Visual Studio. Look at the swagger. Try to execute it. 401 unauthenticated; This is normal. It's a protected API.
Copy the command in swagger (for instance:"https://localhost:44359/WeatherForecast") and save in Notepad.
- Observe the appsettings.json.
- Copy the TenantId to Notepad.
- Observe the startup.cs (web API), and the controller (Authorize attribute, scope verification)
- Open the app registration portal https://portal.azure.com
- In Azure Active Directory | Application registration, find the application
- Look at the Expose an API page, and copy the scopes (for instance: "api://4d36eb86-0d46-4090-9283-c42dc9f149ef/access_as_user")
At this point, you should have three copied items, the URL to the web API from swagger, the scopes from the Azure Portal, and the TenantId Guid from the web API
-
In Visual Studio right click on the solution and Select Open in terminal
mkdir webApp
cd webApp
-
Create the web app by typing
dotnet new webapp --auth SingleOrg --called-api-url
followed by the URL of your web API, and followed by--called-api-scopes
followed by the scopes of your web API. This will be something like this:dotnet new webapp --auth SingleOrg --called-api-url https://localhost:44359/WeatherForecast --called-api-scopes api://4d36eb86-0d46-4090-9283-c42dc9f149ef/access_as_user
-
Still in the console add the new project to the solution using
dotnet sln (add)
:dotnet sln ..\myWebAPI.sln add .\myWebApp.csproj
-
Install the command line tool
dotnet tool install --global msidentity-app-sync
-
Register and create the web app in the Azure AD portal and update the code by providing the same TenantId from the web API, so they are both registered in the same tenant.
msidentity-app-sync --tenant-id testprovisionningtool.onmicrosoft.com
- Observe the appsettings.json. See the client secret, right click on the project, it's under "Manage User Secrets".
- and the startup.cs
- and the index.cshtml.cs page which calls the web API.
- Setup the startup project in the solution to start both projects
- Run the solution. The web app signs-in the user and displays the result of the API.