Skip to content

Commit 920de28

Browse files
authored
Merge pull request #233604 from rwike77/updategraphcode
Updated .net code for new version of Microsoft Graph SDK
2 parents fe7a825 + 1eea7bf commit 920de28

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

articles/active-directory/develop/multi-service-web-app-access-microsoft-graph-as-app.md

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

145145
```dotnetcli
146146
dotnet add package Microsoft.Identity.Web.MicrosoftGraph
147+
dotnet add package Microsoft.Graph
147148
```
148149

149150
#### Package Manager Console
@@ -153,6 +154,7 @@ Open the project/solution in Visual Studio, and open the console by using the **
153154
Run the install commands.
154155
```powershell
155156
Install-Package Microsoft.Identity.Web.MicrosoftGraph
157+
Install-Package Microsoft.Graph
156158
```
157159

158160
### Example
@@ -162,9 +164,9 @@ using System;
162164
using System.Collections.Generic;
163165
using System.Threading.Tasks;
164166
using Microsoft.AspNetCore.Mvc.RazorPages;
165-
using Azure.Identity;​
166-
using Microsoft.Graph.Core;​​
167-
using System.Net.Http.Headers;
167+
using Microsoft.Extensions.Logging;
168+
using Microsoft.Graph;
169+
using Azure.Identity;
168170

169171
...
170172

@@ -178,27 +180,18 @@ public async Task OnGetAsync()
178180
var credential = new ChainedTokenCredential(
179181
new ManagedIdentityCredential(),
180182
new EnvironmentCredential());
181-
var token = credential.GetToken(
182-
new Azure.Core.TokenRequestContext(
183-
new[] { "https://graph.microsoft.com/.default" }));
184183

185-
var accessToken = token.Token;
186-
var graphServiceClient = new GraphServiceClient(
187-
new DelegateAuthenticationProvider((requestMessage) =>
188-
{
189-
requestMessage
190-
.Headers
191-
.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
184+
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
192185

193-
return Task.CompletedTask;
194-
}));
186+
var graphServiceClient = new GraphServiceClient(
187+
credential, scopes);
195188

196-
// MSGraphUser is a DTO class being used to hold User information from the graph service client call
197189
List<MSGraphUser> msGraphUsers = new List<MSGraphUser>();
198190
try
199191
{
200-
var users =await graphServiceClient.Users.Request().GetAsync();
201-
foreach(var u in users)
192+
//var users = await graphServiceClient.Users.Request().GetAsync();
193+
var users = await graphServiceClient.Users.GetAsync();
194+
foreach (var u in users.Value)
202195
{
203196
MSGraphUser user = new MSGraphUser();
204197
user.userPrincipalName = u.UserPrincipalName;
@@ -209,7 +202,7 @@ public async Task OnGetAsync()
209202
msGraphUsers.Add(user);
210203
}
211204
}
212-
catch(Exception ex)
205+
catch (Exception ex)
213206
{
214207
string msg = ex.Message;
215208
}

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)