Skip to content

Commit 921854f

Browse files
committed
updated .net code for new Microsoft Graph SDK
1 parent c49a3d6 commit 921854f

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

articles/app-service/scenario-secure-app-access-microsoft-graph-as-app.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Run the install commands.
4040

4141
```dotnetcli
4242
dotnet add package Microsoft.Identity.Web.MicrosoftGraph
43+
dotnet add package Microsoft.Graph
4344
```
4445

4546
#### Package Manager Console
@@ -49,6 +50,7 @@ Open the project/solution in Visual Studio, and open the console by using the **
4950
Run the install commands.
5051
```powershell
5152
Install-Package Microsoft.Identity.Web.MicrosoftGraph
53+
Install-Package Microsoft.Graph
5254
```
5355

5456
### .NET Example
@@ -58,9 +60,9 @@ using System;
5860
using System.Collections.Generic;
5961
using System.Threading.Tasks;
6062
using Microsoft.AspNetCore.Mvc.RazorPages;
61-
using Azure.Identity;​
62-
using Microsoft.Graph.Core;​​
63-
using System.Net.Http.Headers;
63+
using Microsoft.Extensions.Logging;
64+
using Microsoft.Graph;
65+
using Azure.Identity;
6466

6567
...
6668

@@ -74,27 +76,18 @@ public async Task OnGetAsync()
7476
var credential = new ChainedTokenCredential(
7577
new ManagedIdentityCredential(),
7678
new EnvironmentCredential());
77-
var token = credential.GetToken(
78-
new Azure.Core.TokenRequestContext(
79-
new[] { "https://graph.microsoft.com/.default" }));
8079

81-
var accessToken = token.Token;
82-
var graphServiceClient = new GraphServiceClient(
83-
new DelegateAuthenticationProvider((requestMessage) =>
84-
{
85-
requestMessage
86-
.Headers
87-
.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
80+
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
8881

89-
return Task.CompletedTask;
90-
}));
82+
var graphServiceClient = new GraphServiceClient(
83+
credential, scopes);
9184

92-
// MSGraphUser is a DTO class being used to hold User information from the graph service client call
9385
List<MSGraphUser> msGraphUsers = new List<MSGraphUser>();
9486
try
9587
{
96-
var users =await graphServiceClient.Users.Request().GetAsync();
97-
foreach(var u in users)
88+
//var users = await graphServiceClient.Users.Request().GetAsync();
89+
var users = await graphServiceClient.Users.GetAsync();
90+
foreach (var u in users.Value)
9891
{
9992
MSGraphUser user = new MSGraphUser();
10093
user.userPrincipalName = u.UserPrincipalName;
@@ -105,7 +98,7 @@ public async Task OnGetAsync()
10598
msGraphUsers.Add(user);
10699
}
107100
}
108-
catch(Exception ex)
101+
catch (Exception ex)
109102
{
110103
string msg = ex.Message;
111104
}

0 commit comments

Comments
 (0)