Skip to content

Commit 2074945

Browse files
author
Tiago Brenck
committed
Switched to User.Read.All so only admin can consent
1 parent b768dd5 commit 2074945

File tree

6 files changed

+10
-10
lines changed

6 files changed

+10
-10
lines changed

2-WebApp-graph-user/2-3-Multi-Tenant/AppCreationScripts/Configure.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ Function ConfigureApplications
223223
# Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
224224
Write-Host "Getting access from 'webApp' to 'Microsoft Graph'"
225225
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
226-
-requiredDelegatedPermissions "Directory.Read.All" `
226+
-requiredDelegatedPermissions "User.Read.All" `
227227

228228
$requiredResourcesAccess.Add($requiredPermissions)
229229

2-WebApp-graph-user/2-3-Multi-Tenant/AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"RequiredResourcesAccess": [
2525
{
2626
"Resource": "Microsoft Graph",
27-
"DelegatedPermissions": [ "Directory.Read.All" ]
27+
"DelegatedPermissions": [ "User.Read.All" ]
2828
}
2929
]
3030
}

2-WebApp-graph-user/2-3-Multi-Tenant/Controllers/TodoListController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task<IActionResult> Create(TodoItem model)
8585
}
8686

8787
[HttpGet]
88-
[AuthorizeForScopes(Scopes = new string[] { GraphScope.DirectoryReadAll })]
88+
[AuthorizeForScopes(Scopes = new string[] { GraphScope.UserReadAll })]
8989
public async Task<IActionResult> Edit(int id)
9090
{
9191
TodoItem todoItem = await _todoItemService.Get(id, User);
@@ -99,7 +99,7 @@ public async Task<IActionResult> Edit(int id)
9999
var userTenant = User.GetTenantId();
100100

101101
// Acquiring token for graph using the user's tenantId, so it can return all the users from their tenant
102-
var graphAccessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new string[] { GraphScope.DirectoryReadAll }, userTenant);
102+
var graphAccessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new string[] { GraphScope.UserReadAll }, userTenant);
103103

104104
TempData["UsersDropDown"] = (await _msGraphService.GetUsersAsync(graphAccessToken))
105105
.Select(u => new SelectListItem

2-WebApp-graph-user/2-3-Multi-Tenant/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ As a first step you'll need to:
147147
- Click the **Add a permission** button and then,
148148
- Ensure that the **Microsoft APIs** tab is selected.
149149
- In the *Commonly used Microsoft APIs* section, click on **Microsoft Graph**
150-
- In the **Delegated permissions** section, select the **Directory.Read.All** in the list. Use the search box if necessary.
150+
- In the **Delegated permissions** section, select the **User.Read.All** in the list. Use the search box if necessary.
151151
- Click on the **Add permissions** button in the bottom.
152152

153153
##### Configure the project (WebApp-OpenIDConnect-DotNet) to use your app registration
@@ -294,15 +294,15 @@ If a multi-tenant app needs to acquire a token from Graph to read data from the
294294
```csharp
295295
var userTenant = User.GetTenantId();
296296
// Acquiring token for graph using the user's tenantId, so it can return all the users from their tenant
297-
var graphAccessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new string[] { GraphScope.DirectoryReadAll }, userTenant);
297+
var graphAccessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new string[] { GraphScope.UserReadAll }, userTenant);
298298
```
299299

300-
We are acquiring an access token for Graph with the scope `Directory.Read.All`, to list all the users from the tenant so you can assign a todo item to them. `GetAccessTokenOnBehalfOfUserAsync` is a helper method found on `Microsoft.Identity.Web` project, and it receives a **tenantId** as parameter to acquire a token for the desired authority. For that, we get the current authority from the built `IConfidentialClientApplication` and replace the tenantId. Below is an example of this logic.
300+
We are acquiring an access token for Graph with the scope `User.Read.All`, to list all the users from the tenant so you can assign a todo item to them. `GetAccessTokenOnBehalfOfUserAsync` is a helper method found on `Microsoft.Identity.Web` project, and it receives a **tenantId** as parameter to acquire a token for the desired authority. For that, we get the current authority from the built `IConfidentialClientApplication` and replace the tenantId. Below is an example of this logic.
301301

302302
```csharp
303303
string signedUserAuthority = confidentialClientApplication.Authority.Replace(new Uri(confidentialClientApplication.Authority).PathAndQuery, $"/{tenant}/");
304304
AuthenticationResult result = await confidentialClientApplication
305-
.AcquireTokenSilent(new string[] { "Directory.Read.All" }, account)
305+
.AcquireTokenSilent(new string[] { "User.Read.All" }, account)
306306
.WithAuthority(signedUserAuthority)
307307
.ExecuteAsync()
308308
.ConfigureAwait(false);

2-WebApp-graph-user/2-3-Multi-Tenant/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ConfigureServices(IServiceCollection services)
7474

7575
// Sign-in users with the Microsoft identity platform
7676
services.AddMicrosoftIdentityPlatformAuthentication(Configuration)
77-
.AddMsal(Configuration, new string[] { GraphScope.DirectoryReadAll })
77+
.AddMsal(Configuration, new string[] { GraphScope.UserReadAll })
7878
.AddInMemoryTokenCaches();
7979

8080
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>

2-WebApp-graph-user/2-3-Multi-Tenant/Utils/GraphScope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public static class GraphScope
44
{
5-
public const string DirectoryReadAll = "Directory.Read.All";
5+
public const string UserReadAll = "User.Read.All";
66
}
77
}

0 commit comments

Comments
 (0)