Skip to content

Commit ca20746

Browse files
author
Kalyan Krishna
committed
Working draft after Nuget updates and change to MSAL
1 parent a0a99a4 commit ca20746

File tree

13 files changed

+305
-247
lines changed

13 files changed

+305
-247
lines changed

AppCreationScripts/Cleanup.ps1

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
param([Parameter(Mandatory=$false)][PSCredential]$Credential=$null, [Parameter(Mandatory=$false)][string]$TenantId)
1+
[CmdletBinding()]
2+
param(
3+
[PSCredential] $Credential,
4+
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5+
[string] $tenantId
6+
)
7+
8+
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
9+
Install-Module "AzureAD" -Scope CurrentUser
10+
}
211
Import-Module AzureAD
312
$ErrorActionPreference = 'Stop'
413

@@ -8,15 +17,7 @@ Function Cleanup
817
.Description
918
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
1019
#>
11-
[CmdletBinding()]
12-
param(
13-
[Parameter(HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
14-
[PSCredential] $Credential,
15-
[string] $tenantId
16-
)
1720

18-
process
19-
{
2021
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
2122
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
2223

@@ -49,22 +50,25 @@ This function removes the Azure AD applications for the sample. These applicatio
4950
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5051

5152
Write-Host "Removing 'service' (TodoListService-ManualJwt) if needed"
52-
$app=Get-AzureADApplication -Filter "identifierUris/any(uri:uri eq 'https://$tenantName/TodoListService-ManualJwt')"
53-
if ($app)
53+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-ManualJwt'"
55+
if ($apps)
5456
{
55-
Remove-AzureADApplication -ObjectId $app.ObjectId
56-
Write-Host "Removed."
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
5758
}
59+
Get-AzureRmADServicePrincipal -SearchString "TodoListService-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
60+
Write-Host "Removed TodoListService-ManualJwt."
5861

5962
Write-Host "Removing 'client' (TodoListClient-ManualJwt) if needed"
60-
$app=Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'"
61-
if ($app)
63+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
64+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-ManualJwt'"
65+
if ($apps)
6266
{
63-
Remove-AzureADApplication -ObjectId $app.ObjectId
64-
Write-Host "Removed."
67+
Remove-AzureADApplication -ObjectId $apps.ObjectId
6568
}
69+
Get-AzureRmADServicePrincipal -SearchString "TodoListClient-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
70+
Write-Host "Removed TodoListClient-ManualJwt."
6671

67-
}
6872
}
6973

7074
Cleanup -Credential $Credential -tenantId $TenantId

AppCreationScripts/Configure.ps1

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[CmdletBinding()]
2-
param(
3-
[PSCredential] $Credential,
4-
[Parameter(HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5-
[string] $tenantId
6-
)
2+
param(
3+
[PSCredential] $Credential,
4+
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5+
[string] $tenantId
6+
)
77

88
<#
99
This script creates the Azure AD applications needed for this sample and updates the configuration files
@@ -15,10 +15,6 @@
1515
2) in the PowerShell window, type: Install-Module AzureAD
1616
1717
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
18-
19-
# Parameters
20-
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
21-
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
2218
#>
2319

2420
# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
@@ -66,7 +62,7 @@ Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requ
6662
{
6763
AddResourcePermission $requiredAccess -exposedPermissions $sp.Oauth2Permissions -requiredAccesses $requiredDelegatedPermissions -permissionType "Scope"
6864
}
69-
65+
7066
# $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
7167
if ($requiredApplicationPermissions)
7268
{
@@ -103,7 +99,12 @@ Function ConfigureApplications
10399
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
104100
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
105101
so that they are consistent with the Applications parameters
106-
#>
102+
#>
103+
104+
$commonendpoint = "common"
105+
106+
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
107+
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
107108

108109
# Login to Azure PowerShell (interactive if credentials are not already provided:
109110
# you'll need to sign-in with creds enabling your to create apps in the tenant)
@@ -127,72 +128,81 @@ Function ConfigureApplications
127128
{
128129
$tenantId = $creds.Tenant.Id
129130
}
131+
130132
$tenant = Get-AzureADTenantDetail
131133
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
132134

135+
# Get the user running the script
136+
$user = Get-AzureADUser -ObjectId $creds.Account.Id
137+
133138
# Create the service AAD application
134-
Write-Host "Creating the AAD appplication (TodoListService-ManualJwt)"
139+
Write-Host "Creating the AAD application (TodoListService-ManualJwt)"
135140
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService-ManualJwt" `
136141
-HomePage "https://localhost:44324" `
137-
-IdentifierUris "https://$tenantName/TodoListService-ManualJwt" `
138142
-PublicClient $False
139-
143+
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
144+
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -IdentifierUris $serviceIdentifierUri
140145

141146
$currentAppId = $serviceAadApplication.AppId
142147
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
143148

144-
# add this user as app owner
145-
$user = Get-AzureADUser -ObjectId $creds.Account.Id
146-
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
147-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceAadApplication.DisplayName)'"
149+
# add the user running the script as an app owner if needed
150+
$owner = Get-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId
151+
if ($owner -eq $null)
152+
{
153+
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
154+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
155+
}
148156

149-
Write-Host "Done."
157+
Write-Host "Done creating the service application (TodoListService-ManualJwt)"
150158

151159
# URL of the AAD application in the Azure portal
152-
$servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_IAM/ApplicationBlade/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId
160+
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
161+
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
153162
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService-ManualJwt</a></td></tr>" -Path createdApps.html
154163

155164
# Create the client AAD application
156-
Write-Host "Creating the AAD appplication (TodoListClient-ManualJwt)"
165+
Write-Host "Creating the AAD application (TodoListClient-ManualJwt)"
157166
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient-ManualJwt" `
158-
-ReplyUrls "https://TodoListClient-ManualJwt" `
167+
-ReplyUrls "urn:ietf:wg:oauth:2.0:oob", "https://login.microsoftonline.com/common/oauth2/nativeclient" `
159168
-PublicClient $True
160169

161-
162170
$currentAppId = $clientAadApplication.AppId
163171
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
164172

165-
# add this user as app owner
166-
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
167-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
173+
# add the user running the script as an app owner if needed
174+
$owner = Get-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId
175+
if ($owner -eq $null)
176+
{
177+
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
178+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
179+
}
168180

169-
Write-Host "Done."
181+
Write-Host "Done creating the client application (TodoListClient-ManualJwt)"
170182

171183
# URL of the AAD application in the Azure portal
172-
$clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_IAM/ApplicationBlade/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId
184+
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
185+
$clientPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
173186
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient-ManualJwt</a></td></tr>" -Path createdApps.html
174187

175188
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
189+
176190
# Add Required Resources Access (from 'client' to 'service')
177191
Write-Host "Getting access from 'client' to 'service'"
178192
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService-ManualJwt" `
179-
-requiredDelegatedPermissions "user_impersonation";
193+
-requiredDelegatedPermissions "user_impersonation" `
194+
180195
$requiredResourcesAccess.Add($requiredPermissions)
181-
Set-AzureADApplication -ObjectId $clientAadApplication.ObjectId -RequiredResourceAccess $requiredResourcesAccess
182-
Write-Host "Granted."
183196

184-
# Configure known client applications for service
185-
Write-Host "Configure known client applications for the 'service'"
186-
$knowApplications = New-Object System.Collections.Generic.List[System.String]
187-
$knowApplications.Add($clientAadApplication.AppId)
188-
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -KnownClientApplications $knowApplications
189-
Write-Host "Configured."
197+
198+
Set-AzureADApplication -ObjectId $clientAadApplication.ObjectId -RequiredResourceAccess $requiredResourcesAccess
199+
Write-Host "Granted permissions."
190200

191201
# Update config file for 'service'
192202
$configFile = $pwd.Path + "\..\TodoListService-ManualJwt\Web.Config"
193203
Write-Host "Updating the sample code ($configFile)"
194204
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue $tenantName
195-
ReplaceSetting -configFilePath $configFile -key "ida:Audience" -newValue $serviceAadApplication.IdentifierUris
205+
ReplaceSetting -configFilePath $configFile -key "ida:Audience" -newValue $serviceIdentifierUri
196206
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $serviceAadApplication.AppId
197207

198208
# Update config file for 'client'
@@ -201,12 +211,17 @@ Function ConfigureApplications
201211
ReplaceSetting -configFilePath $configFile -key "ida:Tenant" -newValue $tenantName
202212
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $clientAadApplication.AppId
203213
ReplaceSetting -configFilePath $configFile -key "ida:RedirectUri" -newValue $clientAadApplication.ReplyUrls
204-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue $serviceAadApplication.IdentifierUris
214+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue $serviceIdentifierUri
205215
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
206-
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
207216

217+
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
208218
}
209219

220+
# Pre-requisites
221+
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
222+
Install-Module "AzureAD" -Scope CurrentUser
223+
}
224+
Import-Module AzureAD
210225

211226
# Run interactively (will ask you for the tenant ID)
212227
ConfigureApplications -Credential $Credential -tenantId $TenantId

AppCreationScripts/apps.json renamed to AppCreationScripts/sample.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"Sample": {
3-
"Title": "Manually validating a JWT access token in a web API",
3+
"Title": "How to manually validating a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)",
44
"Level": 300,
5-
"Client": ".NET 4.5 Desktop App (WPF)",
5+
"Client": ".NET Desktop App (WPF)",
66
"Service": "ASP.NET Web API",
7-
"RepositoryUrl": "active-directory-dotnet-webapi-manual-jwt-validation"
7+
"RepositoryUrl": "active-directory-dotnet-webapi-manual-jwt-validation",
8+
"Endpoint": "AAD v2.0"
89
},
910

1011
/*
@@ -14,14 +15,17 @@
1415
{
1516
"Id": "service",
1617
"Name": "TodoListService-ManualJwt",
17-
"Kind": "WebApi",
1818
"IsPublicClient": false,
19+
"Kind": "WebApi",
20+
"Audience": "AzureADMyOrg",
1921
"HomePage": "https://localhost:44324"
2022
},
2123
{
2224
"Id": "client",
2325
"Name": "TodoListClient-ManualJwt",
2426
"Kind": "Desktop",
27+
"Audience": "AzureADMyOrg",
28+
"ReplyUrls": "urn:ietf:wg:oauth:2.0:oob,https://login.microsoftonline.com/common/oauth2/nativeclient",
2529
"IsPublicClient": true,
2630
"RequiredResourcesAccess": [
2731
{
@@ -50,7 +54,7 @@
5054
},
5155
{
5256
"key": "ida:Audience",
53-
"value": "service.IdentifierUris"
57+
"value": "$serviceIdentifierUri"
5458
},
5559
{
5660
"key": "ida:ClientId",
@@ -78,7 +82,7 @@
7882
},
7983
{
8084
"key": "todo:TodoListResourceId",
81-
"value": "service.HomePage"
85+
"value": "$serviceIdentifierUri"
8286
},
8387
{
8488
"key": "todo:TodoListBaseAddress",

TodoListClient/App.config

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
</startup>
66
<appSettings>
77
<add key="ida:Tenant" value="[Enter tenant name, e.g. contoso.onmicrosoft.com]" />
8-
<add key="ida:ClientId" value="[Enter client ID as obtained from Azure Portal, e.g. 82692da5-a86f-44c9-9d53-2f88d52b478b]" />
9-
<add key="ida:RedirectUri" value="[Enter redirect URI as entered in Azure Portal, e.g. http://TodoListClient]" />
10-
<add key="todo:TodoListResourceId" value="[Enter App ID URI of TodoListService, e.g. https://contoso.onmicrosoft.com/TodoListService-ManualJwt]" />
11-
<add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />
8+
<add key="ida:ClientId" value="[Enter client ID of the TodoListClient-ManualJwt as obtained from Azure Portal, e.g. 82692da5-a86f-44c9-9d53-2f88d52b478b]" />
9+
<!--<add key="ida:RedirectUri" value="[Enter redirect URI as entered in Azure Portal, e.g. http://TodoListClient]" />-->
10+
<add key="todo:TodoListResourceId" value="[Enter App ID URI of TodoListService-ManualJwt, e.g. https://contoso.onmicrosoft.com/TodoListService-ManualJwt]" />
11+
<add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}/v2.0" />
1212
<add key="todo:TodoListBaseAddress" value="https://localhost:44324" />
13+
<add key="todo:TodoListScope" value="user_impersonation"/>
1314
</appSettings>
1415
</configuration>

0 commit comments

Comments
 (0)