Skip to content

Commit 9a13b34

Browse files
author
Tiago Brenck
committed
Rollback previous sample as is
1 parent 385d671 commit 9a13b34

29 files changed

+44
-1321
lines changed

1-WebApp-OIDC/1-2-AnyOrg/AppCreationScripts/AppCreationScripts.md

Lines changed: 2 additions & 2 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 Azure Active Directory applications and updating the configuration files for this sample using PowerShell scripts
22

33
## Overview
44

@@ -9,7 +9,7 @@
99
```PowerShell
1010
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1111
```
12-
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)
12+
1. Run the script to create your Azure AD application and configure the code of the sample application accordinly. (Other ways of running the scripts are described below)
1313
```PowerShell
1414
.\AppCreationScripts\Configure.ps1
1515
```

1-WebApp-OIDC/1-2-AnyOrg/AppCreationScripts/Cleanup.ps1

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ param(
55
[string] $tenantId
66
)
77

8-
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
8+
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = "Stop"
12+
$ErrorActionPreference = 'Stop'
1313

1414
Function Cleanup
1515
{
@@ -44,27 +44,20 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'webApp' (WebApp) if needed"
53-
Get-AzureADApplication -Filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'WebApp'"
55-
if ($apps)
56-
{
57-
Remove-AzureADApplication -ObjectId $apps.ObjectId
58-
}
53+
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp'"
5954

60-
foreach ($app in $apps)
55+
if ($app)
6156
{
6257
Remove-AzureADApplication -ObjectId $app.ObjectId
63-
Write-Host "Removed WebApp.."
58+
Write-Host "Removed."
6459
}
65-
# also remove service principals of this app
66-
Get-AzureADServicePrincipal -filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67-
60+
6861
}
6962

70-
Cleanup -Credential $Credential -tenantId $TenantId
63+
Cleanup -Credential $Credential -tenantId $TenantId

1-WebApp-OIDC/1-2-AnyOrg/AppCreationScripts/Configure.ps1

Lines changed: 6 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -17,87 +17,6 @@ param(
1717
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
1818
#>
1919

20-
# Create a password that can be used as an application key
21-
Function ComputePassword
22-
{
23-
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
24-
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
25-
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
26-
$aesManaged.BlockSize = 128
27-
$aesManaged.KeySize = 256
28-
$aesManaged.GenerateKey()
29-
return [System.Convert]::ToBase64String($aesManaged.Key)
30-
}
31-
32-
# Create an application key
33-
# See https://www.sabin.io/blog/adding-an-azure-active-directory-application-and-key-using-powershell/
34-
Function CreateAppKey([DateTime] $fromDate, [double] $durationInYears, [string]$pw)
35-
{
36-
$endDate = $fromDate.AddYears($durationInYears)
37-
$keyId = (New-Guid).ToString();
38-
$key = New-Object Microsoft.Open.AzureAD.Model.PasswordCredential
39-
$key.StartDate = $fromDate
40-
$key.EndDate = $endDate
41-
$key.Value = $pw
42-
$key.KeyId = $keyId
43-
return $key
44-
}
45-
46-
# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
47-
# The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
48-
# described in $permissionType
49-
Function AddResourcePermission($requiredAccess, `
50-
$exposedPermissions, [string]$requiredAccesses, [string]$permissionType)
51-
{
52-
foreach($permission in $requiredAccesses.Trim().Split("|"))
53-
{
54-
foreach($exposedPermission in $exposedPermissions)
55-
{
56-
if ($exposedPermission.Value -eq $permission)
57-
{
58-
$resourceAccess = New-Object Microsoft.Open.AzureAD.Model.ResourceAccess
59-
$resourceAccess.Type = $permissionType # Scope = Delegated permissions | Role = Application permissions
60-
$resourceAccess.Id = $exposedPermission.Id # Read directory data
61-
$requiredAccess.ResourceAccess.Add($resourceAccess)
62-
}
63-
}
64-
}
65-
}
66-
67-
#
68-
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
69-
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
70-
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
71-
{
72-
# If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
73-
if ($servicePrincipal)
74-
{
75-
$sp = $servicePrincipal
76-
}
77-
else
78-
{
79-
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationDisplayName'"
80-
}
81-
$appid = $sp.AppId
82-
$requiredAccess = New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess
83-
$requiredAccess.ResourceAppId = $appid
84-
$requiredAccess.ResourceAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.ResourceAccess]
85-
86-
# $sp.Oauth2Permissions | Select Id,AdminConsentDisplayName,Value: To see the list of all the Delegated permissions for the application:
87-
if ($requiredDelegatedPermissions)
88-
{
89-
AddResourcePermission $requiredAccess -exposedPermissions $sp.Oauth2Permissions -requiredAccesses $requiredDelegatedPermissions -permissionType "Scope"
90-
}
91-
92-
# $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
93-
if ($requiredApplicationPermissions)
94-
{
95-
AddResourcePermission $requiredAccess -exposedPermissions $sp.AppRoles -requiredAccesses $requiredApplicationPermissions -permissionType "Role"
96-
}
97-
return $requiredAccess
98-
}
99-
100-
10120
Function UpdateLine([string] $line, [string] $value)
10221
{
10322
$index = $line.IndexOf('=')
@@ -137,16 +56,13 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
13756
Set-Content -Value "<html><body><table>" -Path createdApps.html
13857
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
13958

140-
$ErrorActionPreference = "Stop"
141-
14259
Function ConfigureApplications
14360
{
14461
<#.Description
14562
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
14663
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
14764
so that they are consistent with the Applications parameters
14865
#>
149-
$commonendpoint = "common"
15066

15167
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
15268
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
@@ -177,74 +93,52 @@ Function ConfigureApplications
17793
$tenant = Get-AzureADTenantDetail
17894
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
17995

180-
# Get the user running the script to add the user as the app owner
96+
# Get the user running the script
18197
$user = Get-AzureADUser -ObjectId $creds.Account.Id
18298

18399
# Create the webApp AAD application
184100
Write-Host "Creating the AAD application (WebApp)"
185-
# Get a 2 years application key for the webApp Application
186-
$pw = ComputePassword
187-
$fromDate = [DateTime]::Now;
188-
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
189-
$webAppAppKey = $pw
190-
# create the application
191101
$webAppAadApplication = New-AzureADApplication -DisplayName "WebApp" `
192102
-HomePage "https://localhost:44321/" `
193103
-LogoutUrl "https://localhost:44321/signout-oidc" `
194104
-ReplyUrls "https://localhost:44321/", "https://localhost:44321/signin-oidc" `
195105
-IdentifierUris "https://$tenantName/WebApp" `
196106
-AvailableToOtherTenants $True `
197-
-PasswordCredentials $key `
198107
-Oauth2AllowImplicitFlow $true `
199108
-PublicClient $False
200109

201-
# create the service principal of the newly created application
202110
$currentAppId = $webAppAadApplication.AppId
203111
$webAppServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
204112

205113
# add the user running the script as an app owner if needed
206114
$owner = Get-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId
207115
if ($owner -eq $null)
208116
{
209-
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
210-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
117+
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
118+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
211119
}
212120

213-
214121
Write-Host "Done creating the webApp application (WebApp)"
215122

216123
# URL of the AAD application in the Azure portal
217124
# Future? $webAppPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
218125
$webAppPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
219126
Add-Content -Value "<tr><td>webApp</td><td>$currentAppId</td><td><a href='$webAppPortalUrl'>WebApp</a></td></tr>" -Path createdApps.html
220127

221-
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
222-
223-
# Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
224-
Write-Host "Getting access from 'webApp' to 'Microsoft Graph'"
225-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
226-
-requiredDelegatedPermissions "Directory.Read.All" `
227-
228-
$requiredResourcesAccess.Add($requiredPermissions)
229-
230-
231-
Set-AzureADApplication -ObjectId $webAppAadApplication.ObjectId -RequiredResourceAccess $requiredResourcesAccess
232-
Write-Host "Granted permissions."
233128

234129
# Update config file for 'webApp'
235130
$configFile = $pwd.Path + "\..\appsettings.json"
236131
Write-Host "Updating the sample code ($configFile)"
237-
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = "organizations";"Domain" = $tenantName;"ClientSecret" = $webAppAppKey };
132+
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = "organizations";"Domain" = $tenantName };
238133
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
239-
134+
240135
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
241136
}
242137

243138
# Pre-requisites
244139
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
245140
Install-Module "AzureAD" -Scope CurrentUser
246-
}
247-
141+
}
248142
Import-Module AzureAD
249143

250144
# Run interactively (will ask you for the tenant ID)

1-WebApp-OIDC/1-2-AnyOrg/AppCreationScripts/sample.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
"HomePage": "https://localhost:44321/",
2121
"ReplyUrls": "https://localhost:44321/, https://localhost:44321/signin-oidc",
2222
"LogoutUrl": "https://localhost:44321/signout-oidc",
23-
"PasswordCredentials": "Auto",
24-
"RequiredResourcesAccess": [
25-
{
26-
"Resource": "Microsoft Graph",
27-
"DelegatedPermissions": [ "Directory.Read.All" ]
28-
}
29-
]
3023
}
3124
],
3225

@@ -53,10 +46,6 @@
5346
{
5447
"key": "Domain",
5548
"value": "$tenantName"
56-
},
57-
{
58-
"key": "ClientSecret",
59-
"value": ".AppKey"
6049
}
6150
]
6251
}

1-WebApp-OIDC/1-2-AnyOrg/BLL/IMSGraphService.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

1-WebApp-OIDC/1-2-AnyOrg/BLL/ITodoItemService.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

1-WebApp-OIDC/1-2-AnyOrg/BLL/MSGraphService.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)