Skip to content

Commit f3ef83d

Browse files
committed
Extending the scope of the sample:
- Accepting both the ClientId or the App ID URI of the service as audiences - Accepting both Azure AD V1 and Azure AD V2 issuers as issuers for the token (See # - for this adding an ida:ClientId setting in the Web.Config - Updating the readme accordinly - updating the app.json / Configure.ps1 script accordinly.
1 parent 515f1fc commit f3ef83d

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

AppCreationScripts/Configure.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Function ConfigureApplications
198198
Write-Host "Updating the sample code ($configFile)"
199199
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue $tenantName
200200
ReplaceSetting -configFilePath $configFile -key "ida:Audience" -newValue $serviceAadApplication.IdentifierUris
201+
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $serviceAadApplication.AppId
201202

202203
# Update config file for 'client'
203204
$configFile = $pwd.Path + "\..\TodoListClient\App.Config"

AppCreationScripts/apps.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
{
4242
"key": "ida:Audience",
4343
"value": "service.IdentifierUris"
44+
},
45+
{
46+
"key": "ida:ClientId",
47+
"value": "service.AppId"
4448
}
4549
]
4650
},

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ client: .NET Framework 4.5 WPF
77
service: .NET Framework 4.5 Web Api
88
endpoint: AAD V1
99
---
10-
![Build badge](https://identitydivision.visualstudio.com/_apis/public/build/definitions/a7934fdd-dcde-4492-a406-7fad6ac00e17/18/badge)
11-
1210
# Manually validating a JWT access token in a web API
1311

12+
![Build badge](https://identitydivision.visualstudio.com/_apis/public/build/definitions/a7934fdd-dcde-4492-a406-7fad6ac00e17/18/badge)
13+
1414
## About this sample
1515

1616
This sample demonstrates how to manually process a JWT access token in a web API using the JSON Web Token Handler For the Microsoft .Net Framework 4.5. This sample is equivalent to the [NativeClient-DotNet](https://github.com/Azure-Samples/active-directory-dotnet-native-desktop) sample, except that, in the ``TodoListService``, instead of using OWIN middleware to process the token, the token is processed manually in application code. The client, which demonstrates how to acquire a token for this protected API, is unchanged from the [NativeClient-DotNet](https://github.com/Azure-Samples/active-directory-dotnet-native-desktop) sample.
@@ -96,12 +96,13 @@ here are two projects in this sample. Each needs to be separately registered in
9696
2. Open the `web.config` file.
9797
3. Find the app key `ida:Tenant` and replace the value with your AAD tenant name.
9898
4. Find the app key `ida:Audience` and replace the value with the App ID URI you registered earlier, for example `https://<your_tenant_name>/TodoListService-ManualJwt`.
99+
5. Find the app key `ida:ClientId` and replace the value with the **Application ID** (also named Clientid) you copied earlier to the clipboard for this service application.
99100

100101
#### Configure the TodoListClient project
101102

102103
1. Open `app.config`.
103104
2. Find the app key `ida:Tenant` and replace the value with your AAD tenant name.
104-
3. Find the app key `ida:ClientId` and replace the value with the Client ID for the TodoListClient from the Azure portal.
105+
3. Find the app key `ida:ClientId` and replace the value with the Application ID (also named) Client ID for the TodoListClient from the Azure portal.
105106
4. Find the app key `ida:RedirectUri` and replace the value with the Redirect URI for the TodoListClient from the Azure portal, for example `https://TodoListClient`.
106107
5. Find the app key `todo:TodoListResourceId` and replace the value with the App ID URI of the TodoListService-ManualJwt project, for example `https://<your_tenant_name>/TodoListService-ManualJwt`
107108
6. Find the app key `todo:TodoListBaseAddress` and replace the value with the base address of the TodoListService-ManualJwt project, for example `https://localhost:44324`.

TodoListService-ManualJwt/Global.asax.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ internal class TokenValidationHandler : DelegatingHandler
6666
static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
6767
static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
6868
static string audience = ConfigurationManager.AppSettings["ida:Audience"];
69+
static string audience = ConfigurationManager.AppSettings["ida:AppId"];
6970
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
7071

7172
static string _issuer = string.Empty;
@@ -124,8 +125,11 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
124125

125126
TokenValidationParameters validationParameters = new TokenValidationParameters
126127
{
127-
ValidAudience = audience,
128-
ValidIssuer = issuer,
128+
// We accept both the App Id URI and the AppId of this service application
129+
ValidAudiences = new[] { audience, appId },
130+
131+
// Supports both the Azure AD V1 and V2 endpoint
132+
ValidIssuers = new [] { issuer, $"{issuer}/v2.0" },
129133
IssuerSigningTokens = signingTokens,
130134
CertificateValidator = X509CertificateValidator.None // Certificate validation does not make sense since AAD's metadata document is signed with a self-signed certificate.
131135
};

TodoListService-ManualJwt/Web.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<add key="webpages:Enabled" value="false" />
1010
<add key="ClientValidationEnabled" value="true" />
1111
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
12+
<add key="ida:ClientId" value="[Enter the Application Id (also named ClientId) for the application]" />
1213
<add key="ida:Tenant" value="[Enter tenant name, e.g. contoso.onmicrosoft.com]" />
1314
<add key="ida:Audience" value="[Enter App ID URI of TodoListService-ManualJwt, e.g. https://skwantoso.com/TodoListService-ManualJwt]" />
1415
<add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />

0 commit comments

Comments
 (0)