Skip to content

Commit d0ea916

Browse files
author
Kalyan Krishna
committed
More bug fixes
1 parent 77ba4ff commit d0ea916

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

3-WebApp-multi-APIs/AppCreationScripts/Cleanup.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ Function Cleanup
3535
# Removes the applications
3636
Write-Host "Cleaning-up applications from tenant '$tenantId'"
3737

38-
Write-Host "Removing 'webApp' (WebApp) if needed"
38+
Write-Host "Removing 'webApp' (Azure_Api_WebApp) if needed"
3939
try
4040
{
41-
Get-MgApplication -Filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
41+
Get-MgApplication -Filter "DisplayName eq 'Azure_Api_WebApp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
4242
}
4343
catch
4444
{
4545
$message = $_
4646
Write-Warning $Error[0]
47-
Write-Host "Unable to remove the application 'WebApp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
47+
Write-Host "Unable to remove the application 'Azure_Api_WebApp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
4848
}
4949

50-
Write-Host "Making sure there are no more (WebApp) applications found, will remove if needed..."
51-
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
50+
Write-Host "Making sure there are no more (Azure_Api_WebApp) applications found, will remove if needed..."
51+
$apps = Get-MgApplication -Filter "DisplayName eq 'Azure_Api_WebApp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
5252

5353
if ($apps)
5454
{
@@ -58,19 +58,19 @@ Function Cleanup
5858
foreach ($app in $apps)
5959
{
6060
Remove-MgApplication -ApplicationId $app.Id -Debug
61-
Write-Host "Removed WebApp.."
61+
Write-Host "Removed Azure_Api_WebApp.."
6262
}
6363

6464
# also remove service principals of this app
6565
try
6666
{
67-
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
67+
Get-MgServicePrincipal -filter "DisplayName eq 'Azure_Api_WebApp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
6868
}
6969
catch
7070
{
7171
$message = $_
7272
Write-Warning $Error[0]
73-
Write-Host "Unable to remove ServicePrincipal 'WebApp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
73+
Write-Host "Unable to remove ServicePrincipal 'Azure_Api_WebApp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
7474
}
7575
}
7676

3-WebApp-multi-APIs/AppCreationScripts/Configure.ps1

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,40 @@ Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requ
8585
}
8686

8787

88+
Function UpdateLine([string] $line, [string] $value)
89+
{
90+
$index = $line.IndexOf(':')
91+
$lineEnd = ''
92+
93+
if($line[$line.Length - 1] -eq ','){ $lineEnd = ',' }
94+
95+
if ($index -ige 0)
96+
{
97+
$line = $line.Substring(0, $index+1) + " " + '"' + $value+ '"' + $lineEnd
98+
}
99+
return $line
100+
}
101+
102+
Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable] $dictionary)
103+
{
104+
$lines = Get-Content $configFilePath
105+
$index = 0
106+
while($index -lt $lines.Length)
107+
{
108+
$line = $lines[$index]
109+
foreach($key in $dictionary.Keys)
110+
{
111+
if ($line.Contains($key))
112+
{
113+
$lines[$index] = UpdateLine $line $dictionary[$key]
114+
}
115+
}
116+
$index++
117+
}
118+
119+
Set-Content -Path $configFilePath -Value $lines -Force
120+
}
121+
88122
Function ConfigureApplications
89123
{
90124
$isOpenSSl = 'N' #temporary disable open certificate creation
@@ -112,14 +146,14 @@ Function ConfigureApplications
112146

113147

114148
# Create the webApp AAD application
115-
Write-Host "Creating the AAD application (WebApp)"
149+
Write-Host "Creating the AAD application (Azure_Api_WebApp)"
116150
# Get a 6 months application key for the webApp Application
117151
$fromDate = [DateTime]::Now;
118152
$key = CreateAppKey -fromDate $fromDate -durationInMonths 6
119153

120154

121155
# create the application
122-
$webAppAadApplication = New-MgApplication -DisplayName "WebApp" `
156+
$webAppAadApplication = New-MgApplication -DisplayName "Azure_Api_WebApp" `
123157
-Web `
124158
@{ `
125159
RedirectUris = "https://localhost:44321/", "https://localhost:44321/signin-oidc"; `
@@ -133,7 +167,7 @@ Function ConfigureApplications
133167
$webAppAppKey = $pwdCredential.SecretText
134168

135169
$tenantName = (Get-MgApplication -ApplicationId $webAppAadApplication.Id).PublisherDomain
136-
Update-MgApplication -ApplicationId $webAppAadApplication.Id -IdentifierUris @("https://$tenantName/WebApp")
170+
Update-MgApplication -ApplicationId $webAppAadApplication.Id -IdentifierUris @("https://$tenantName/Azure_Api_WebApp")
137171

138172
# create the service principal of the newly created application
139173
$currentAppId = $webAppAadApplication.AppId
@@ -146,35 +180,40 @@ Function ConfigureApplications
146180
New-MgApplicationOwnerByRef -ApplicationId $webAppAadApplication.Id -BodyParameter = @{"@odata.id" = "htps://graph.microsoft.com/v1.0/directoryObjects/$user.ObjectId"}
147181
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
148182
}
149-
Write-Host "Done creating the webApp application (WebApp)"
183+
Write-Host "Done creating the webApp application (Azure_Api_WebApp)"
150184

151185
# URL of the AAD application in the Azure portal
152186
# Future? $webAppPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.Id+"/isMSAApp/"
153187
$webAppPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.Id+"/isMSAApp/"
154-
Add-Content -Value "<tr><td>webApp</td><td>$currentAppId</td><td><a href='$webAppPortalUrl'>WebApp</a></td></tr>" -Path createdApps.html
188+
Add-Content -Value "<tr><td>webApp</td><td>$currentAppId</td><td><a href='$webAppPortalUrl'>Azure_Api_WebApp</a></td></tr>" -Path createdApps.html
155189
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphRequiredResourceAccess]
156190

157191
# Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
158192
Write-Host "Getting access from 'webApp' to 'Microsoft Graph'"
159-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
193+
$requiredPermission = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
160194
-requiredDelegatedPermissions "User.Read" `
161-
$requiredResourcesAccess.Add($requiredPermissions)
195+
196+
$requiredResourcesAccess.Add($requiredPermission)
162197

163198
# Add Required Resources Access (from 'webApp' to 'Windows Azure Service Management API')
164199
Write-Host "Getting access from 'webApp' to 'Windows Azure Service Management API'"
165-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Windows Azure Service Management API" `
200+
$requiredPermission = GetRequiredPermissions -applicationDisplayName "Windows Azure Service Management API" `
166201
-requiredDelegatedPermissions "user_impersonation" `
167-
$requiredResourcesAccess.Add($requiredPermissions)
202+
203+
$requiredResourcesAccess.Add($requiredPermission)
168204

169205
# Add Required Resources Access (from 'webApp' to 'Azure Storage')
170206
Write-Host "Getting access from 'webApp' to 'Azure Storage'"
171-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Azure Storage" `
207+
$requiredPermission = GetRequiredPermissions -applicationDisplayName "Azure Storage" `
172208
-requiredDelegatedPermissions "user_impersonation" `
173-
$requiredResourcesAccess.Add($requiredPermissions)
209+
210+
$requiredResourcesAccess.Add($requiredPermission)
174211
Update-MgApplication -ApplicationId $webAppAadApplication.Id -RequiredResourceAccess $requiredResourcesAccess
175212
Write-Host "Granted permissions."
176213

177-
Write-Host "Successfully registered and configured that app registration for 'WebApp' at" -ForegroundColor Green
214+
Write-Host "Successfully registered and configured that app registration for 'Azure_Api_WebApp' at" -ForegroundColor Green
215+
216+
# print the registered app portal URL for any further navigation
178217
$webAppPortalUrl
179218

180219
# Update config file for 'webApp'
@@ -186,6 +225,7 @@ Function ConfigureApplications
186225
Write-Host "Updating the sample config '$configFile' with the following config values"
187226
$dictionary
188227

228+
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
189229
if($isOpenSSL -eq 'Y')
190230
{
191231
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"

3-WebApp-multi-APIs/AppCreationScripts/sample.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"AADApps": [
2121
{
2222
"Id": "webApp",
23-
"Name": "WebApp",
23+
"Name": "Azure_Api_WebApp",
2424
"Kind": "WebApp",
2525
"HomePage": "https://localhost:44321/",
2626
"ReplyUrls": "https://localhost:44321/, https://localhost:44321/signin-oidc",
@@ -52,7 +52,7 @@
5252
"CodeConfiguration": [
5353
{
5454
"App": "webApp",
55-
"SettingKind": "JSon",
55+
"SettingKind": "JSON",
5656
"SettingFile": "\\..\\appsettings.json",
5757
"Mappings": [
5858
{

3-WebApp-multi-APIs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
page_type: sample
33
services: ms-identity
44
client: ASP.NET Core Web App
5-
service: Azure resource Manager
5+
service: Azure REST Api
66
level: 200
77
languages:
88
- aspnetcore

0 commit comments

Comments
 (0)