685e85eb7d | ||
---|---|---|
.. | ||
images | ||
README.md |
README.md
Authentication
The .NET Podcast can be configured to use authentication from an OAuth provider. The feeds
route has been configured to allow updates and deletions only from authenticated clients. This guide assumes that Azure Active Directory will be used as the authentication provider.
Prerequisites
-
An Azure account with an active subscription. If you don't have an Azure subscription, create a free account before you begin.
-
Deploy the dotnet-podcasts application. Follow the detailed guidelines here.
-
Azure Active Directory has been configured for a domain. Follow the Create and configure an Azure Active Directory Domain Services managed domain tutorial, if you do not currently have domain services configured.
-
The .NET CLI will need to be installed and configured. See the .NET CLI overview for more information and how to install the .NET SDK.
Configure Azure Active Directory
-
Sign in to the Azure Portal
-
Select the
Azure Active Directory
option from the side menu -
Select the
App registrations
option from the Azure Active Directory side menu. -
Select the
New registration
option from the Azure Active Directory top menu. -
On the
Register an application
page, provide a name for the application and selectRegister
. -
On the app registration overview page, take note of the
Application (client) ID
andDirectory (tenant) ID
as we will need this information for a later step. -
Select the
Manage > App Roles
side menu option from the app registration management page. -
Select the
Create app role
option from the App roles top menu. -
Configure the app role with the following information and select
Apply
.Configuration Value Display Name API Access
Allowed member types Both (Users/Groups + Applications)
Value API.Access
Description Allows a user to edit in the api.
Do you want to enable this app role? Enabled
Enable Authentication in the Podcast.API
-
In the
Podcast.API
project, open theappsettings.json
(or environment specific file such asappsettings.Development.json
). -
Add a new top-level property of
AzureAd
with the following configurations. The{DOMAIN}
,{AZURE_AD_TENANT_ID}
, and{AZURE_AD_CLIENT_ID}
will need to be replaced with values captured earlier when configuring your app registration.{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "ConnectionStrings": { "PodcastDb": "Server=localhost, 5433;Database=Podcast;User Id=sa;Password=Pass@word;Encrypt=False", "FeedQueue": "UseDevelopmentStorage=true" }, "AzureAd": { "Instance": "https://login.microsoftonline.com/", "Domain": "{DOMAIN}", "TenantId": "{AZURE_AD_TENANT_ID}", "ClientId": "{AZURE_AD_CLIENT_ID}" }, "Authentication": { "Schemes": { "Bearer": { "ValidAudiences": ["1ba2c41d-3a54-414a-9700-1f9393cfafca"], "ValidIssuer": "dotnet-user-jwts" } } } }
-
Uncomment the following line from the
Program.cs
file.builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
-
Start debugging or deploying your application.
Testing Configurations
-
Using a terminal with .NET CLI available, navigate to the folder that contains the
Podcast.API
project and execute the following command. The{TENANT_ID}
parameter will need to be replaced with the tenant id captured earlier when configuring your app registration.dotnet user-jwts create --audience 1ba2c41d-3a54-414a-9700-1f9393cfafca --claim "scp=API.Access" --claim "tid={TENANT_ID}"
-
When this executes, it will generate a new
Token
. Select the token value for later use. -
Navigate to the
Podcast.API
swagger page. If debugging it may behttps://localhost:5001/swagger/index.html
. -
Select the
Authorize
button, add theToken
value generated previously, selectAuthorize
, and selectClose
. -
Using the swagger UI, expand the
Get /feeds
option andExecute
. From the response body, select theid
value from one of the returned feed items. -
Using the swagger UI, expand the
Delete /feeds
option, select thetry it out
option, paste theid
copied from the previous step into theid
field, andExecute
. If everything is authenticated successfully, you will receive a204
code.