Skip to content

Commit 88b0b70

Browse files
committed
update scope name
1 parent e2018e1 commit 88b0b70

File tree

7 files changed

+23
-60
lines changed

7 files changed

+23
-60
lines changed

5-AccessControl/1-call-api-roles/API/TodoListAPI/Controllers/TodoListController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public TodoListController(TodoContext context)
2727
// GET: api/todolist/getAll
2828
[HttpGet]
2929
[Route("getAll")]
30-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Read")]
30+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
3131
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskAdminRoleRequired)]
3232
public async Task<ActionResult<IEnumerable<TodoItem>>> GetAll()
3333
{
@@ -36,7 +36,7 @@ public async Task<ActionResult<IEnumerable<TodoItem>>> GetAll()
3636

3737
// GET: api/TodoItems
3838
[HttpGet]
39-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Read")]
39+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
4040
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
4141
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
4242
{
@@ -57,7 +57,7 @@ public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
5757

5858
// GET: api/TodoItems/5
5959
[HttpGet("{id}")]
60-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Read")]
60+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
6161
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
6262
public async Task<ActionResult<TodoItem>> GetTodoItem(int id)
6363
{
@@ -68,7 +68,7 @@ public async Task<ActionResult<TodoItem>> GetTodoItem(int id)
6868
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
6969
// more details see https://aka.ms/RazorPagesCRUD.
7070
[HttpPut("{id}")]
71-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Write")]
71+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
7272
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
7373
public async Task<IActionResult> PutTodoItem(int id, TodoItem todoItem)
7474
{
@@ -106,7 +106,7 @@ public async Task<IActionResult> PutTodoItem(int id, TodoItem todoItem)
106106
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
107107
// more details see https://aka.ms/RazorPagesCRUD.
108108
[HttpPost]
109-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Write")]
109+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
110110
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
111111
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
112112
{
@@ -121,7 +121,7 @@ public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
121121

122122
// DELETE: api/TodoItems/5
123123
[HttpDelete("{id}")]
124-
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Write")]
124+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
125125
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
126126
public async Task<ActionResult<TodoItem>> DeleteTodoItem(int id)
127127
{

5-AccessControl/1-call-api-roles/API/TodoListAPI/appsettings.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"Domain": "Enter the domain of your Azure AD tenant, e.g. 'contoso.onmicrosoft.com'",
55
"TenantId": "Enter the tenant ID",
66
"ClientId": "Enter the Client ID (aka 'Application ID')",
7-
"Scopes": {
8-
"Read": ["TodoList.Read", "TodoList.ReadWrite"],
9-
"Write": ["TodoList.ReadWrite"]
10-
},
7+
"Scopes": ["access_as_user"],
118
"Roles": {
129
"TaskAdmin": "TaskAdmin",
1310
"TaskUser": "TaskUser"

5-AccessControl/1-call-api-roles/AppCreationScripts/Configure.ps1

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,7 @@ Function ConfigureApplications
221221
}
222222

223223
$scopes = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphPermissionScope]
224-
$scope = CreateScope -value TodoList.Read `
225-
-userConsentDisplayName "Access msal-angular-app" `
226-
-userConsentDescription "Allow the application to access msal-angular-app on your behalf." `
227-
-adminConsentDisplayName "Access msal-angular-app" `
228-
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of an admin."
229-
230-
$scopes.Add($scope)
231-
$scope = CreateScope -value TodoList.ReadWrite `
224+
$scope = CreateScope -value access_as_user `
232225
-userConsentDisplayName "Access msal-angular-app" `
233226
-userConsentDescription "Allow the application to access msal-angular-app on your behalf." `
234227
-adminConsentDisplayName "Access msal-angular-app" `
@@ -249,7 +242,7 @@ Function ConfigureApplications
249242
# Add Required Resources Access (from 'client' to 'client')
250243
Write-Host "Getting access from 'client' to 'client'"
251244
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "msal-angular-app" `
252-
-requiredDelegatedPermissions "TodoList.Read|TodoList.ReadWrite" `
245+
-requiredDelegatedPermissions "access_as_user" `
253246

254247

255248
$requiredResourcesAccess.Add($requiredPermissions)
@@ -266,7 +259,7 @@ Function ConfigureApplications
266259

267260
# Update config file for 'client'
268261
$configFile = $pwd.Path + "\..\SPA\src\app\auth-config.ts"
269-
$dictionary = @{ "Enter_the_Application_Id_Here" = $clientAadApplication.AppId;"Enter_the_Tenant_Info_Here" = $tenantId;"Enter_the_Web_Api_Scope_here" = ("api://"+$clientAadApplication.AppId+"/access_as_user") };
262+
$dictionary = @{ "Enter_the_Application_Id_Here" = $clientAadApplication.AppId;"Enter_the_Tenant_Info_Here" = $tenantId;"Enter_the_Web_Api_Application_Id_Here" = $clientAadApplication.AppId };
270263

271264
Write-Host "Updating the sample code ($configFile)"
272265

5-AccessControl/1-call-api-roles/AppCreationScripts/sample.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Service": ".NET Core web API",
77
"RepositoryUrl": "ms-identity-javascript-angular-tutorial",
88
"Endpoint": "AAD v2.0",
9-
"Languages": ["TypeScript", "CSharp"],
9+
"Languages": ["typescript", "csharp"],
1010
"Description": "Angular single-page application calling a protected web API using App Roles to implement Role-Based Access Control",
1111
"products": ["azure-active-directory", "msal-js", "msal-angular", "microsoft-identity-web"]
1212
},
@@ -18,21 +18,15 @@
1818
"Audience": "AzureADMyOrg",
1919
"HomePage": "http://localhost:4200/",
2020
"ReplyUrls": "http://localhost:4200/, http://localhost:4200/auth",
21-
"Scopes": [
22-
"TodoList.Read",
23-
"TodoList.ReadWrite"
24-
],
21+
"Scopes": ["access_as_user"],
2522
"Sample": {
2623
"SampleSubPath": "5-AccessControl\\1-call-api-roles\\SPA",
2724
"ProjectDirectory": "\\1-call-api-roles\\SPA"
2825
},
2926
"RequiredResourcesAccess": [
3027
{
3128
"Resource": "client",
32-
"DelegatedPermissions": [
33-
"TodoList.Read",
34-
"TodoList.ReadWrite"
35-
]
29+
"DelegatedPermissions": ["access_as_user"]
3630
}
3731
],
3832
"AppRoles": [
@@ -95,8 +89,8 @@
9589
"value": "$tenantId"
9690
},
9791
{
98-
"key": "Enter_the_Web_Api_Scope_here",
99-
"value": "client.Scope"
92+
"key": "Enter_the_Web_Api_Application_Id_Here",
93+
"value": "client.AppId"
10094
}
10195
]
10296
}

5-AccessControl/1-call-api-roles/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,14 @@ To manually register the apps, as a first step you'll need to:
163163
164164
1. All APIs must publish a minimum of one [scope](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-authorization-code), also called [Delegated Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client apps to obtain an access token for a *user* successfully. To publish a scope, follow these steps:
165165
1. Select **Add a scope** button open the **Add a scope** screen and Enter the values as indicated below:
166-
1. For **Scope name**, use `TodoList.Read`.
166+
1. For **Scope name**, use `access_as_user`.
167167
1. Select **Admins and users** options for **Who can consent?**.
168168
1. For **Admin consent display name** type in the details, `e.g. Allow the users of the app msal-angular-app to read ToDo list items`.
169169
1. For **Admin consent description** type in the details `e.g. Allows the app msal-angular-app to read the signed-in users ToDo list items.`
170-
1. For **User consent display name** type in the details `e.g. Read ToDo list items as yourself`.
171-
1. For **User consent description** type in the details `e.g. Allow the app msal-angular-app to read ToDo list items on your behalf.`
170+
1. For **User consent display name** type in the details `e.g. Read Todolist items as yourself`.
171+
1. For **User consent description** type in the details `e.g. Allow the app msal-angular-app to read Todolist items on your behalf.`
172172
1. Keep **State** as **Enabled**.
173173
1. Select the **Add scope** button on the bottom to save this scope.
174-
> Repeat the steps above for another scope named **TodoList.ReadWrite**
175174
1. Select the **Manifest** blade on the left.
176175
1. Set `accessTokenAcceptedVersion` property to **2**.
177176
1. Select on **Save**.
@@ -186,7 +185,7 @@ To manually register the apps, as a first step you'll need to:
186185
1. Ensure that the **My APIs** tab is selected.
187186
1. In the list of APIs, select the API `msal-angular-app`.
188187
* Since this app signs-in users, we will now proceed to select **delegated permissions**, which is is requested by apps when signing-in users.
189-
1. In the **Delegated permissions** section, select the **TodoList.Read**, **TodoList.ReadWrite** in the list. Use the search box if necessary.
188+
1. In the **Delegated permissions** section, select the **access_as_user** in the list. Use the search box if necessary.
190189
1. Select the **Add permissions** button at the bottom.
191190
192191
##### Publish Application Roles for users and groups
@@ -222,7 +221,7 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
222221
1. Open the `SPA\src\app\auth-config.ts` file.
223222
1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `msal-angular-app` app copied from the Azure portal.
224223
1. Find the key `Enter_the_Tenant_Info_Here` and replace the existing value with your Azure AD tenant ID.
225-
1. Find the key `Enter_the_Web_Api_Scope_here` and replace the existing value with Scope.
224+
1. Find the key `Enter_the_Web_Api_Application_Id_Here` and replace the existing value with the application ID (clientId) of the web API -in this scenario, this is the same application ID with `msal-angular-app`.
226225
227226
### Step 6: Running the sample
228227

5-AccessControl/1-call-api-roles/SPA/src/app/app.module.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,7 @@ export function MSALInstanceFactory(): IPublicClientApplication {
4646
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
4747
const protectedResourceMap = new Map<string, Array<string | ProtectedResourceScopes> | null>();
4848

49-
protectedResourceMap.set(protectedResources.apiTodoList.endpoint, [
50-
{
51-
httpMethod: 'GET',
52-
scopes: [...protectedResources.apiTodoList.scopes.read]
53-
},
54-
{
55-
httpMethod: 'POST',
56-
scopes: [...protectedResources.apiTodoList.scopes.write]
57-
},
58-
{
59-
httpMethod: 'PUT',
60-
scopes: [...protectedResources.apiTodoList.scopes.write]
61-
},
62-
{
63-
httpMethod: 'DELETE',
64-
scopes: [...protectedResources.apiTodoList.scopes.write]
65-
}
66-
]);
49+
protectedResourceMap.set(protectedResources.apiTodoList.endpoint, [...protectedResources.apiTodoList.scopes]);
6750

6851
return {
6952
interactionType: InteractionType.Popup,

5-AccessControl/1-call-api-roles/SPA/src/app/auth-config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ export const msalConfig: Configuration = {
4848
export const protectedResources = {
4949
apiTodoList: {
5050
endpoint: "https://localhost:44351/api/todolist",
51-
scopes: {
52-
read: ["api://Enter_the_Web_Api_Application_Id_Here/TodoList.Read"],
53-
write: ["api://Enter_the_Web_Api_Application_Id_Here/TodoList.ReadWrite"]
54-
}
51+
scopes: ["api://Enter_the_Web_Api_Application_Id_Here/access_as_user"]
5552
}
5653
}
5754

@@ -62,7 +59,7 @@ export const protectedResources = {
6259
* https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes
6360
*/
6461
export const loginRequest = {
65-
scopes: []
62+
scopes: [...protectedResources.apiTodoList.scopes]
6663
};
6764

6865
export const roles = {

0 commit comments

Comments
 (0)