Skip to content

Commit 82728c9

Browse files
Shama-KKalyan KrishnaTiago Brenckjmprieur
authored
Updated api pattern and target framework (#140)
* Updated api pattern and target framework * api pattern_not mandatory requirement * Updated graph api calls using Graph SDK * Check client Id to access API * Removed manual step to add platform * Made a few fixes * Updated the pattern for custom OnTokenValidated event * small improvements * README sync with code generator * Adjusted KCA text * Updated Readme files and topologies * Rename the AddPop method (#146) into AddProofOfPosession * Update for Merge error Co-authored-by: Kalyan Krishna <kalyan.krishna@microsoft.com> Co-authored-by: Tiago Brenck <v-tibre@microsoft.com> Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
1 parent e367a85 commit 82728c9

File tree

32 files changed

+1122
-932
lines changed

32 files changed

+1122
-932
lines changed

1. Desktop app calls Web API/README-incremental.md

Lines changed: 361 additions & 0 deletions
Large diffs are not rendered by default.

1. Desktop app calls Web API/README.md

Lines changed: 143 additions & 323 deletions
Large diffs are not rendered by default.
-1.97 KB
Loading

1. Desktop app calls Web API/TodoListService/TodoListService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Identity.Web" Version="0.1.5-preview" />
10+
<PackageReference Include="Microsoft.Identity.Web" Version="0.1.2-preview" />
1111
</ItemGroup>
1212
</Project>

2. Web API now calls Microsoft Graph/AppCreationScripts/AppCreationScripts.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Registering the sample apps with Microsoft Identity Platform and updating the configuration files using PowerShell scripts
1+
# Registering the sample apps with Microsoft identity platform and updating the configuration files using PowerShell scripts
22

33
## Overview
44

@@ -11,7 +11,8 @@
1111
```
1212
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. (Other ways of running the scripts are described below)
1313
```PowerShell
14-
.\AppCreationScripts\Configure.ps1
14+
cd .\AppCreationScripts\
15+
.\Configure.ps1
1516
```
1617
1. Open the Visual Studio solution and click start
1718

@@ -26,6 +27,7 @@ The following paragraphs:
2627
- [Passing credentials](#option-2-non-interactive) to create the app in your home tenant
2728
- [Interactively in a specific tenant](#option-3-interactive-but-create-apps-in-a-specified-tenant)
2829
- [Passing credentials in a specific tenant](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
30+
- [Passing environment name, for Sovereign clouds](#running-the-script-on-azure-sovereign-clouds)
2931

3032
## Goal of the scripts
3133

@@ -49,7 +51,7 @@ These scripts are:
4951

5052
The `Configure.ps1` will stop if it tries to create an Azure AD application which already exists in the tenant. For this, if you are using the script to try/test the sample, or in DevOps scenarios, you might want to run `Cleanup.ps1` just before `Configure.ps1`. This is what is shown in the steps below.
5153

52-
## How to use the app creation scripts ?
54+
## How to use the app creation scripts?
5355

5456
### Pre-requisites
5557

@@ -107,7 +109,7 @@ Note that the script will choose the tenant in which to create the applications,
107109
108110
#### Option 2 (non-interactive)
109111
110-
When you know the indentity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window
112+
When you know the identity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window
111113
112114
```PowerShell
113115
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
@@ -144,3 +146,21 @@ $tenantId = "yourTenantIdGuid"
144146
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
145147
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
146148
```
149+
150+
### Running the script on Azure Sovereign clouds
151+
152+
All the four options listed above, can be used on any Azure Sovereign clouds. By default, the script targets `AzureCloud`, but it can be changed using the parameter `-AzureEnvironmentName`.
153+
154+
The acceptable values for this parameter are:
155+
156+
- AzureCloud
157+
- AzureChinaCloud
158+
- AzureUSGovernment
159+
- AzureGermanyCloud
160+
161+
Example:
162+
163+
```PowerShell
164+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
165+
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
166+
```

2. Web API now calls Microsoft Graph/AppCreationScripts/Cleanup.ps1

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
param(
33
[PSCredential] $Credential,
44
[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
5+
[string] $tenantId,
6+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script (it defaults to AzureCloud)')]
7+
[string] $azureEnvironmentName
68
)
79

10+
#Requires -Modules AzureAD
11+
12+
813
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
914
Install-Module "AzureAD" -Scope CurrentUser
1015
}
@@ -13,10 +18,15 @@ $ErrorActionPreference = "Stop"
1318

1419
Function Cleanup
1520
{
16-
<#
17-
.Description
18-
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
19-
#>
21+
if (!$azureEnvironmentName)
22+
{
23+
$azureEnvironmentName = "AzureCloud"
24+
}
25+
26+
<#
27+
.Description
28+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
29+
#>
2030

2131
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
2232
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -25,17 +35,17 @@ This function removes the Azure AD applications for the sample. These applicatio
2535
# you'll need to sign-in with creds enabling your to create apps in the tenant)
2636
if (!$Credential -and $TenantId)
2737
{
28-
$creds = Connect-AzureAD -TenantId $tenantId
38+
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
2939
}
3040
else
3141
{
3242
if (!$TenantId)
3343
{
34-
$creds = Connect-AzureAD -Credential $Credential
44+
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
3545
}
3646
else
3747
{
38-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
48+
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
3949
}
4050
}
4151

@@ -49,9 +59,9 @@ This function removes the Azure AD applications for the sample. These applicatio
4959
# Removes the applications
5060
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5161

52-
Write-Host "Removing 'service' (TodoListService (active-directory-dotnet-native-aspnetcore-v2)) if needed"
53-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'"
62+
Write-Host "Removing 'service' (TodoListService(ms-identity-dotnet-native-aspnetcore-v2)) if needed"
63+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService(ms-identity-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
64+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService(ms-identity-dotnet-native-aspnetcore-v2)'"
5565
if ($apps)
5666
{
5767
Remove-AzureADApplication -ObjectId $apps.ObjectId
@@ -60,14 +70,14 @@ This function removes the Azure AD applications for the sample. These applicatio
6070
foreach ($app in $apps)
6171
{
6272
Remove-AzureADApplication -ObjectId $app.ObjectId
63-
Write-Host "Removed TodoListService (active-directory-dotnet-native-aspnetcore-v2).."
73+
Write-Host "Removed TodoListService(ms-identity-dotnet-native-aspnetcore-v2).."
6474
}
6575
# also remove service principals of this app
66-
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
76+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListService(ms-identity-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
6777

68-
Write-Host "Removing 'client' (TodoListClient (active-directory-dotnet-native-aspnetcore-v2)) if needed"
69-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
70-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'"
78+
Write-Host "Removing 'client' (TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)) if needed"
79+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
80+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)'"
7181
if ($apps)
7282
{
7383
Remove-AzureADApplication -ObjectId $apps.ObjectId
@@ -76,10 +86,10 @@ This function removes the Azure AD applications for the sample. These applicatio
7686
foreach ($app in $apps)
7787
{
7888
Remove-AzureADApplication -ObjectId $app.ObjectId
79-
Write-Host "Removed TodoListClient (active-directory-dotnet-native-aspnetcore-v2).."
89+
Write-Host "Removed TodoListClient(ms-identity-dotnet-native-aspnetcore-v2).."
8090
}
8191
# also remove service principals of this app
82-
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient (active-directory-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
92+
Get-AzureADServicePrincipal -filter "DisplayName eq 'TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
8393

8494
}
8595

2. Web API now calls Microsoft Graph/AppCreationScripts/Configure.ps1

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
param(
33
[PSCredential] $Credential,
44
[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
5+
[string] $tenantId,
6+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script (it defaults to AzureCloud)')]
7+
[string] $azureEnvironmentName
68
)
79

10+
#Requires -Modules AzureAD
11+
812
<#
913
This script creates the Azure AD applications needed for this sample and updates the configuration files
1014
for the visual Studio projects from the data in the Azure AD applications.
@@ -201,6 +205,11 @@ Function ConfigureApplications
201205
so that they are consistent with the Applications parameters
202206
#>
203207
$commonendpoint = "common"
208+
209+
if (!$azureEnvironmentName)
210+
{
211+
$azureEnvironmentName = "AzureCloud"
212+
}
204213

205214
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
206215
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -209,17 +218,17 @@ Function ConfigureApplications
209218
# you'll need to sign-in with creds enabling your to create apps in the tenant)
210219
if (!$Credential -and $TenantId)
211220
{
212-
$creds = Connect-AzureAD -TenantId $tenantId
221+
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
213222
}
214223
else
215224
{
216225
if (!$TenantId)
217226
{
218-
$creds = Connect-AzureAD -Credential $Credential
227+
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
219228
}
220229
else
221230
{
222-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
231+
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
223232
}
224233
}
225234

@@ -228,21 +237,23 @@ Function ConfigureApplications
228237
$tenantId = $creds.Tenant.Id
229238
}
230239

240+
241+
231242
$tenant = Get-AzureADTenantDetail
232243
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
233244

234245
# Get the user running the script to add the user as the app owner
235246
$user = Get-AzureADUser -ObjectId $creds.Account.Id
236247

237248
# Create the service AAD application
238-
Write-Host "Creating the AAD application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
249+
Write-Host "Creating the AAD application (TodoListService(ms-identity-dotnet-native-aspnetcore-v2))"
239250
# Get a 2 years application key for the service Application
240251
$pw = ComputePassword
241252
$fromDate = [DateTime]::Now;
242253
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
243254
$serviceAppKey = $pw
244255
# create the application
245-
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
256+
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService(ms-identity-dotnet-native-aspnetcore-v2)" `
246257
-HomePage "https://localhost:44351/" `
247258
-ReplyUrls "https://localhost:44351/" `
248259
-AvailableToOtherTenants $True `
@@ -281,9 +292,9 @@ Function ConfigureApplications
281292
{
282293
# Add scope
283294
$scope = CreateScope -value "access_as_user" `
284-
-userConsentDisplayName "Access TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
285-
-userConsentDescription "Allow the application to access TodoListService (active-directory-dotnet-native-aspnetcore-v2) on your behalf." `
286-
-adminConsentDisplayName "Access TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
295+
-userConsentDisplayName "Access TodoListService(ms-identity-dotnet-native-aspnetcore-v2)" `
296+
-userConsentDescription "Allow the application to access TodoListService(ms-identity-dotnet-native-aspnetcore-v2) on your behalf." `
297+
-adminConsentDisplayName "Access TodoListService(ms-identity-dotnet-native-aspnetcore-v2)" `
287298
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of the signed-in user."
288299

289300
$scopes.Add($scope)
@@ -293,12 +304,12 @@ Function ConfigureApplications
293304
# add/update scopes
294305
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -OAuth2Permission $scopes
295306

296-
Write-Host "Done creating the service application (TodoListService (active-directory-dotnet-native-aspnetcore-v2))"
307+
Write-Host "Done creating the service application (TodoListService(ms-identity-dotnet-native-aspnetcore-v2))"
297308

298309
# URL of the AAD application in the Azure portal
299310
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
300311
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
301-
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService (active-directory-dotnet-native-aspnetcore-v2)</a></td></tr>" -Path createdApps.html
312+
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService(ms-identity-dotnet-native-aspnetcore-v2)</a></td></tr>" -Path createdApps.html
302313

303314
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
304315

@@ -314,9 +325,9 @@ Function ConfigureApplications
314325
Write-Host "Granted permissions."
315326

316327
# Create the client AAD application
317-
Write-Host "Creating the AAD application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
328+
Write-Host "Creating the AAD application (TodoListClient(ms-identity-dotnet-native-aspnetcore-v2))"
318329
# create the application
319-
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient (active-directory-dotnet-native-aspnetcore-v2)" `
330+
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)" `
320331
-ReplyUrls "https://login.microsoftonline.com/common/oauth2/nativeclient" `
321332
-AvailableToOtherTenants $True `
322333
-PublicClient $True
@@ -334,18 +345,18 @@ Function ConfigureApplications
334345
}
335346

336347

337-
Write-Host "Done creating the client application (TodoListClient (active-directory-dotnet-native-aspnetcore-v2))"
348+
Write-Host "Done creating the client application (TodoListClient(ms-identity-dotnet-native-aspnetcore-v2))"
338349

339350
# URL of the AAD application in the Azure portal
340351
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
341352
$clientPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
342-
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient (active-directory-dotnet-native-aspnetcore-v2)</a></td></tr>" -Path createdApps.html
353+
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient(ms-identity-dotnet-native-aspnetcore-v2)</a></td></tr>" -Path createdApps.html
343354

344355
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
345356

346357
# Add Required Resources Access (from 'client' to 'service')
347358
Write-Host "Getting access from 'client' to 'service'"
348-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService (active-directory-dotnet-native-aspnetcore-v2)" `
359+
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService(ms-identity-dotnet-native-aspnetcore-v2)" `
349360
-requiredDelegatedPermissions "access_as_user" `
350361

351362
$requiredResourcesAccess.Add($requiredPermissions)
@@ -371,9 +382,9 @@ Function ConfigureApplications
371382
# Update config file for 'client'
372383
$configFile = $pwd.Path + "\..\TodoListClient\App.Config"
373384
Write-Host "Updating the sample code ($configFile)"
374-
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $clientAadApplication.AppId
375-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListScope" -newValue ("api://"+$serviceAadApplication.AppId+"/.default")
376-
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
385+
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue ($clientAadApplication.AppId)
386+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListScope" -newValue (("api://"+$serviceAadApplication.AppId+"/.default"))
387+
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue ($serviceAadApplication.HomePage)
377388
Write-Host ""
378389
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
379390
Write-Host "IMPORTANT: Please follow the instructions below to complete a few manual step(s) in the Azure portal":
@@ -383,7 +394,7 @@ Function ConfigureApplications
383394
Write-Host "- For 'client'"
384395
Write-Host " - Navigate to '$clientPortalUrl'"
385396
Write-Host " - Navigate to the Manifest page and change 'signInAudience' to 'AzureADandPersonalMicrosoftAccount'." -ForegroundColor Red
386-
Write-Host " - [Optional] If you are a tenant admin, you can navigate to the API Permisions page and select 'Grant admin consent for (your tenant)'" -ForegroundColor Red
397+
Write-Host " - [Optional] If you are a tenant admin, you can navigate to the API Permissions page and select 'Grant admin consent for (your tenant)'" -ForegroundColor Red
387398

388399
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
389400

0 commit comments

Comments
 (0)