Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit d8f26c8

Browse files
review changes
1 parent aa0ed92 commit d8f26c8

File tree

5 files changed

+252
-88
lines changed

5 files changed

+252
-88
lines changed

5-AccessControl/2-call-api-groups/API/TodoListAPI/Startup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public void ConfigureServices(IServiceCollection services)
5252

5353
options.Events.OnTokenValidated = async context =>
5454
{
55-
string[] allowedClientApps = { Configuration["AzureAd:ClientId"] }; // In this scenario, client and service share the same clientId
55+
string[] allowedClientApps = { Configuration["AzureAd:ClientId"] }; // In this scenario, client and service share the same clientId and we disallow all calls to this API, except from the SPA
5656

5757
string clientappId = context?.Principal?.Claims
5858
.FirstOrDefault(x => x.Type == "azp" || x.Type == "appid")?.Value;
5959

6060
if (!allowedClientApps.Contains(clientappId))
6161
{
62-
throw new System.Exception("This client is not authorized");
62+
throw new System.Exception("This client is not authorized to call this Api");
6363
}
6464

6565
// calls method to process groups overage claim.
@@ -69,7 +69,7 @@ public void ConfigureServices(IServiceCollection services)
6969
};
7070
}, options => { Configuration.Bind("AzureAd", options); })
7171
.EnableTokenAcquisitionToCallDownstreamApi(options => Configuration.Bind("AzureAd", options))
72-
.AddMicrosoftGraph(Configuration.GetSection("MsGraph"))
72+
.AddMicrosoftGraph(Configuration.GetSection("MSGraph"))
7373
.AddInMemoryTokenCaches();
7474

7575
// The following lines code instruct the asp.net core middleware to use the data in the "roles" claim in the Authorize attribute and User.IsInrole()

5-AccessControl/2-call-api-groups/AppCreationScripts/Cleanup.ps1

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,81 @@ param(
77
[string] $azureEnvironmentName
88
)
99

10+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Groups")) {
11+
Install-Module "Microsoft.Graph.Groups" -Scope CurrentUser
12+
}
13+
14+
Import-Module Microsoft.Graph.Groups
15+
16+
<#.Description
17+
This function creates a new Azure AD Security Group with provided values
18+
#>
19+
Function CreateSecurityGroup([string] $name, [string] $description)
20+
{
21+
Write-Host "Creating a security group by the name '$name'."
22+
$newGroup = New-MgGroup -Description $description -DisplayName $name -MailEnabled:$false -SecurityEnabled:$true -MailNickName $name
23+
return Get-MgGroup -Filter "DisplayName eq '$name'"
24+
}
25+
26+
<#.Description
27+
This function first checks and then creates a new Azure AD Security Group with provided values, if required
28+
#>
29+
Function CreateIfNotExistsSecurityGroup([string] $name, [string] $description, [switch] $promptBeforeCreate)
30+
{
31+
32+
# check if Group exists
33+
$group = Get-MgGroup -Filter "DisplayName eq '$name'"
34+
35+
if( $group -eq $null)
36+
{
37+
if ($promptBeforeCreate)
38+
{
39+
$confirmation = Read-Host "Proceed to create a new security group named '$name' in the tenant ? (Y/N)"
40+
41+
if($confirmation -eq 'y')
42+
{
43+
$group = CreateSecurityGroup -name $name -description $description
44+
}
45+
}
46+
else
47+
{
48+
Write-Host "No Security Group created!"
49+
}
50+
}
51+
52+
return $group
53+
}
54+
55+
<#.Description
56+
This function first checks and then deletes an existing Azure AD Security Group, if required
57+
#>
58+
Function RemoveSecurityGroup([string] $name, [switch] $promptBeforeDelete)
59+
{
60+
61+
# check if Group exists
62+
$group = Get-MgGroup -Filter "DisplayName eq '$name'"
63+
64+
if( $group -ne $null)
65+
{
66+
if ($promptBeforeDelete)
67+
{
68+
$confirmation = Read-Host "Proceed to delete an existing group named '$name' in the tenant ?(Y/N)"
69+
70+
if($confirmation -eq 'y')
71+
{
72+
Remove-MgGroup -GroupId $group.Id
73+
Write-Host "Security group '$name' successfully deleted"
74+
}
75+
}
76+
else
77+
{
78+
Write-Host "No Security group by name '$name' exists in the tenant, no deletion needed."
79+
}
80+
}
81+
82+
return $group.Id
83+
}
84+
1085
Function Cleanup
1186
{
1287
if (!$azureEnvironmentName)
@@ -24,11 +99,13 @@ Function Cleanup
2499

25100
# Connect to the Microsoft Graph API
26101
Write-Host "Connecting to Microsoft Graph"
27-
if ($tenantId -eq "") {
102+
if ($tenantId -eq "")
103+
{
28104
Connect-MgGraph -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
29105
$tenantId = (Get-MgContext).TenantId
30106
}
31-
else {
107+
else
108+
{
32109
Connect-MgGraph -TenantId $tenantId -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
33110
}
34111

@@ -72,16 +149,10 @@ Function Cleanup
72149
Write-Warning $Error[0]
73150
Write-Host "Unable to remove ServicePrincipal 'msal-angular-app'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
74151
}
75-
Write-Host "You may want to remove the security group 'GroupAdmin' if it was created to test this sample only."
76-
#if($null -ne (Get-MgGroup -Filter "DisplayName eq 'GroupAdmin'"))
77-
#{
78-
# Remove-MgGroup -GroupId (Get-MgGroup -Filter "DisplayName eq 'GroupAdmin'").Id
79-
#}
80-
Write-Host "You may want to remove the security group 'GroupMember' if it was created to test this sample only."
81-
#if($null -ne (Get-MgGroup -Filter "DisplayName eq 'GroupMember'"))
82-
#{
83-
# Remove-MgGroup -GroupId (Get-MgGroup -Filter "DisplayName eq 'GroupMember'").Id
84-
#}
152+
153+
# remove security groups, if relevant to the sample
154+
RemoveSecurityGroup -name 'GroupAdmin' -promptBeforeDelete 'Y'
155+
RemoveSecurityGroup -name 'GroupMember' -promptBeforeDelete 'Y'
85156
}
86157

87158
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) {

0 commit comments

Comments
 (0)