Skip to content

Commit 7c3fefd

Browse files
committed
Updates to Graph SDK 5
1 parent 01cfe05 commit 7c3fefd

File tree

24 files changed

+64
-60
lines changed

24 files changed

+64
-60
lines changed

2-WebApp-graph-user/2-1-Call-MSGraph/README-incremental-instructions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The two new lines of code:
122122
123123
### Add additional files to call Microsoft Graph
124124

125-
Add the `Microsoft.Identity.Web.MicrosoftGraph` package, to use [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md).
125+
Add the `Microsoft.Identity.Web.GraphServiceClient` package, to use [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md).
126126
127127
### Update the `Startup.cs` file to enable the Microsoft Graph custom service
128128

@@ -160,13 +160,13 @@ public HomeController(ILogger<HomeController> logger,
160160
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
161161
public async Task<IActionResult> Profile()
162162
{
163-
var me = await _graphServiceClient.Me.Request().GetAsync();
163+
var me = await _graphServiceClient.Me.GetAsync();
164164
ViewData["Me"] = me;
165165

166166
try
167167
{
168168
// Get user photo
169-
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
169+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.GetAsync())
170170
{
171171
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
172172
ViewData["Photo"] = Convert.ToBase64String(photoByte);
@@ -290,15 +290,15 @@ To process the CAE challenge from Microsoft Graph, the controller actions need t
290290
1. Catch a Microsoft Graph SDK's `ServiceException` and extract the required `claims`. This is done by wrapping the call to Microsoft Graph into a try/catch block that processes the challenge:
291291

292292
```CSharp
293-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
293+
currentUser = await _graphServiceClient.Me.GetAsync();
294294
```
295295

296296
1. Then redirect the user back to Azure AD with the new requested `claims`. Azure AD will use this `claims` payload to discern what or if any additional processing is required, example being the user needs to sign-in again or do multi-factor authentication.
297297

298298
```CSharp
299299
try
300300
{
301-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
301+
currentUser = await _graphServiceClient.Me.GetAsync();
302302
}
303303
// Catch CAE exception from Graph SDK
304304
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))

2-WebApp-graph-user/2-1-Call-MSGraph/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
193193
194194
## About The code
195195

196-
1. In this aspnetcore web project, first the packages `Microsoft.Identity.Web`, `Microsoft.Identity.Web.UI` and `Microsoft.Identity.Web.MicrosoftGraph` were added from NuGet. These libraries are used to simplify the process of signing-in a user and acquiring tokens for Microsoft Graph.
196+
1. In this aspnetcore web project, first the packages `Microsoft.Identity.Web`, `Microsoft.Identity.Web.UI` and `Microsoft.Identity.Web.GraphServiceClient` were added from NuGet. These libraries are used to simplify the process of signing-in a user and acquiring tokens for Microsoft Graph.
197197

198198
2. Starting with the **Startup.cs** file :
199199

@@ -242,13 +242,13 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
242242
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
243243
public async Task<IActionResult> Profile()
244244
{
245-
var me = await _graphServiceClient.Me.Request().GetAsync();
245+
var me = await _graphServiceClient.Me.GetAsync();
246246
ViewData["Me"] = me;
247247

248248
try
249249
{
250250
// Get user photo
251-
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
251+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.GetAsync())
252252
{
253253
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
254254
ViewData["Photo"] = Convert.ToBase64String(photoByte);
@@ -509,13 +509,13 @@ To process the CAE challenge from Microsoft Graph, the controller actions need t
509509
1. The process to handle CAE challenges from MS Graph comprises of the following steps:
510510
1. Catch a Microsoft Graph SDK's `ServiceException` and extract the required `claims`. This is done by wrapping the call to Microsoft Graph into a try/catch block that processes the challenge:
511511
```CSharp
512-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
512+
currentUser = await _graphServiceClient.Me.GetAsync();
513513
```
514514
1. Then redirect the user back to Azure AD with the new requested `claims`. Azure AD will use this `claims` payload to discern what or if any additional processing is required, example being the user needs to sign-in again or do multi-factor authentication.
515515
```CSharp
516516
try
517517
{
518-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
518+
currentUser = await _graphServiceClient.Me.GetAsync();
519519
}
520520
// Catch CAE exception from Graph SDK
521521
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))

2-WebApp-graph-user/2-1-Call-MSGraph/ReadmeFiles/CAE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ To process the CAE challenge from Microsoft Graph, the controller actions need t
2929
1. The process to handle CAE challenges from MS Graph comprises of the following steps:
3030
1. Catch a Microsoft Graph SDK's `ServiceException` and extract the required `claims`. This is done by wrapping the call to Microsoft Graph into a try/catch block that processes the challenge:
3131
```CSharp
32-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
32+
currentUser = await _graphServiceClient.Me.GetAsync();
3333
```
3434
1. Then redirect the user back to Azure AD with the new requested `claims`. Azure AD will use this `claims` payload to discern what or if any additional processing is required, example being the user needs to sign-in again or do multi-factor authentication.
3535
```CSharp
3636
try
3737
{
38-
currentUser = await _graphServiceClient.Me.Request().GetAsync();
38+
currentUser = await _graphServiceClient.Me.GetAsync();
3939
}
4040
// Catch CAE exception from Graph SDK
4141
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))

2-WebApp-graph-user/2-1-Call-MSGraph/ReadmeFiles/ExploreSample.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
## About The code
1212

13-
1. In this aspnetcore web project, first the packages `Microsoft.Identity.Web`, `Microsoft.Identity.Web.UI` and `Microsoft.Identity.Web.MicrosoftGraph` were added from NuGet. These libraries are used to simplify the process of signing-in a user and acquiring tokens for Microsoft Graph.
13+
1. In this aspnetcore web project, first the packages `Microsoft.Identity.Web`, `Microsoft.Identity.Web.UI` and `Microsoft.Identity.Web.GraphServiceClient` were added from NuGet. These libraries are used to simplify the process of signing-in a user and acquiring tokens for Microsoft Graph.
1414

1515
2. Starting with the **Startup.cs** file :
1616

@@ -60,13 +60,13 @@
6060
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
6161
public async Task<IActionResult> Profile()
6262
{
63-
var me = await _graphServiceClient.Me.Request().GetAsync();
63+
var me = await _graphServiceClient.Me.GetAsync();
6464
ViewData["Me"] = me;
6565

6666
try
6767
{
6868
// Get user photo
69-
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
69+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.GetAsync())
7070
{
7171
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
7272
ViewData["Photo"] = Convert.ToBase64String(photoByte);

2-WebApp-graph-user/2-2-TokenCache/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public HomeController(ILogger<HomeController> logger,
3232
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
3333
public async Task<IActionResult> Index()
3434
{
35-
var user = await _graphServiceClient.Me.Request().GetAsync();
35+
var user = await _graphServiceClient.Me.GetAsync();
3636
ViewData["ApiResult"] = user.DisplayName;
3737
var encodedCachedTimeUTC = await _cache.GetAsync("cachedTimeUTC");
3838

@@ -42,13 +42,13 @@ public async Task<IActionResult> Index()
4242
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
4343
public async Task<IActionResult> Profile()
4444
{
45-
var me = await _graphServiceClient.Me.Request().GetAsync();
45+
var me = await _graphServiceClient.Me.GetAsync();
4646
ViewData["Me"] = me;
4747

4848
try
4949
{
5050
// Get user photo
51-
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
51+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.GetAsync())
5252
{
5353
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
5454
ViewData["Photo"] = Convert.ToBase64String(photoByte);

2-WebApp-graph-user/2-2-TokenCache/Views/Home/Profile.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</td>
2727
</tr>
2828
@{
29-
var me = ViewData["me"] as Microsoft.Graph.User;
29+
var me = ViewData["me"] as Microsoft.Graph.Models.User;
3030
var properties = me.GetType().GetProperties();
3131
foreach (var child in properties)
3232
{

2-WebApp-graph-user/2-2-TokenCache/WebApp-OpenIDConnect-DotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="3.1.8" />
2323
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.0" />
2424
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
25-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.12.1" />
25+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
2626
</ItemGroup>
2727

2828

2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public HomeController(ILogger<HomeController> logger,
2828
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
2929
public async Task<IActionResult> Index()
3030
{
31-
var user = await _graphServiceClient.Me.Request().GetAsync();
31+
var user = await _graphServiceClient.Me.GetAsync();
3232
ViewData["ApiResult"] = user.DisplayName;
3333

3434
return View();
@@ -37,13 +37,13 @@ public async Task<IActionResult> Index()
3737
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
3838
public async Task<IActionResult> Profile()
3939
{
40-
var me = await _graphServiceClient.Me.Request().GetAsync();
40+
var me = await _graphServiceClient.Me.GetAsync();
4141
ViewData["Me"] = me;
4242

4343
try
4444
{
4545
// Get user photo
46-
using (var photoStream = await _graphServiceClient.Me.Photo.Content.Request().GetAsync())
46+
using (var photoStream = await _graphServiceClient.Me.Photo.Content.GetAsync())
4747
{
4848
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
4949
ViewData["Photo"] = Convert.ToBase64String(photoByte);

2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/Views/Home/Profile.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</td>
2727
</tr>
2828
@{
29-
var me = ViewData["me"] as Microsoft.Graph.User;
29+
var me = ViewData["me"] as Microsoft.Graph.Models.User;
3030
var properties = me.GetType().GetProperties();
3131
foreach (var child in properties)
3232
{

2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/WebApp-OpenIDConnect-DotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
22-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.12.1" />
22+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
2323
</ItemGroup>
2424

2525
</Project>

2-WebApp-graph-user/2-5-HybridFlow/2-5-HybridFlow.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Identity.Web" Version="2.12.1" />
12-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.12.1" />
12+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
1313
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
1414
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.3" />
1515
</ItemGroup>

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/CallGraphBFF.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="7.0.5" />
18-
<PackageReference Include="Microsoft.Identity.Web" Version="2.9.0" />
19-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.9.0" />
20-
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.9.0" />
18+
<PackageReference Include="Microsoft.Identity.Web" Version="2.12.1" />
19+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
20+
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
2121
</ItemGroup>
2222

2323
<ItemGroup>

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/Controllers/ProfileController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Identity.Client;
55
using Microsoft.Identity.Web;
66
using Microsoft.Graph;
7+
using Microsoft.Graph.Models;
78

89
namespace TodoListBFF.Controllers;
910

@@ -25,7 +26,6 @@ public async Task<ActionResult<User>> GetProfile()
2526
try
2627
{
2728
User profile = await _graphServiceClient.Me
28-
.Request()
2929
.GetAsync();
3030

3131
return Ok(profile);

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// Add Microsoft.Identity.Web services to the container.
1717
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration)
1818
.EnableTokenAcquisitionToCallDownstreamApi(builder.Configuration.GetSection("DownstreamApi:Scopes").Value!.Split(' '))
19-
.AddMicrosoftGraph(builder.Configuration.GetValue<string>("DownstreamApi:BaseUrl")!, builder.Configuration.GetValue<string>("DownstreamApi:Scopes")!)
19+
.AddMicrosoftGraph(builder.Configuration.GetValue<string>("DownstreamApi:BaseUrl")!,
20+
builder.Configuration.GetValue<string>("DownstreamApi:Scopes")!.Split(' '))
2021
.AddInMemoryTokenCaches();
2122

2223
// Add session for sharing non-sensitive strings between routes.

4-WebApp-your-API/4-3-AnyOrg/ToDoListClient/ToDoListClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
9-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.12.1" />
9+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
1010
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.1" />
1111
</ItemGroup>
1212

4-WebApp-your-API/4-3-AnyOrg/TodoListService/Controllers/TodoListController.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,23 @@ private async Task<List<string>> CallGraphApiOnBehalfOfUser()
229229
try
230230
{
231231
// Call the Graph API and retrieve the user's profile.
232-
IGraphServiceUsersCollectionPage users =
232+
var users =
233233
await CallGraphWithCAEFallback(
234234
async () =>
235235
{
236-
return await _graphServiceClient.Users.Request()
237-
.Filter($"accountEnabled eq true")
238-
.Select("id, userPrincipalName")
239-
.GetAsync();
236+
return await _graphServiceClient.Users.GetAsync(r =>
237+
{
238+
r.QueryParameters.Filter = "accountEnabled eq true";
239+
r.QueryParameters.Select = new string[] { "id", "userPrincipalName" };
240+
}
241+
);
242+
240243
}
241244
);
242245

243246
if (users != null)
244247
{
245-
return users.Select(x => x.UserPrincipalName).ToList();
248+
return users.Value.Select(x => x.UserPrincipalName).ToList();
246249
}
247250
throw new Exception();
248251
}

4-WebApp-your-API/4-3-AnyOrg/TodoListService/ToDoListService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>
17-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.12.1" />
17+
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.1" />
1818
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.1" />
1919
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.1" />
2020
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.1" Condition="'$(Configuration)' == 'Debug'" />

5-WebApp-AuthZ/5-2-Groups/Controllers/UserProfileController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Graph;
5+
using Microsoft.Graph.Models;
56
using Microsoft.Identity.Client;
67
using Microsoft.Identity.Web;
78
using System;
@@ -33,10 +34,10 @@ public async Task<IActionResult> Index()
3334
{
3435
try
3536
{
36-
User me = await _graphServiceClient.Me.Request().GetAsync();
37+
User me = await _graphServiceClient.Me.GetAsync();
3738
ViewData["Me"] = me;
3839

39-
var photo = await _graphServiceClient.Me.Photo.Request().GetAsync();
40+
var photo = await _graphServiceClient.Me.Photo.GetAsync();
4041
ViewData["Photo"] = photo;
4142
}
4243
// Catch CAE exception from Graph SDK
@@ -53,7 +54,7 @@ public async Task<IActionResult> Index()
5354
_consentHandler.HandleException(ex2);
5455
}
5556
}
56-
catch (ServiceException svcex) when (svcex.Error.Code == "ImageNotFound")
57+
catch (ServiceException svcex) when (svcex.IsMatch("ImageNotFound"))
5758
{
5859
//swallow
5960
}

5-WebApp-AuthZ/5-2-Groups/README-incremental-instructions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ public async Task<IActionResult> Index()
403403
{
404404
try
405405
{
406-
User me = await _graphServiceClient.Me.Request().GetAsync();
406+
User me = await _graphServiceClient.Me.GetAsync();
407407
ViewData["Me"] = me;
408408

409-
var photo = await _graphServiceClient.Me.Photo.Request().GetAsync();
409+
var photo = await _graphServiceClient.Me.Photo.GetAsync();
410410
ViewData["Photo"] = photo;
411411
}
412412
// See 'Optional - Handle Continuous Access Evaluation (CAE) challenge from Microsoft Graph' for more information.
@@ -502,16 +502,16 @@ To process the CAE challenge from Microsoft Graph, the controller actions need t
502502
1. The process to handle CAE challenges from MS Graph comprises of the following steps:
503503
1. Catch a Microsoft Graph SDK's `ServiceException` and extract the required `claims`. This is done by wrapping the call to Microsoft Graph into a try/catch block that processes the challenge:
504504
```CSharp
505-
User me = await _graphServiceClient.Me.Request().GetAsync();
505+
User me = await _graphServiceClient.Me.GetAsync();
506506
```
507507
1. Then redirect the user back to Azure AD with the new requested `claims`. Azure AD will use this `claims` payload to discern what or if any additional processing is required, example being the user needs to sign-in again or do multi-factor authentication.
508508
```CSharp
509509
try
510510
{
511-
User me = await _graphServiceClient.Me.Request().GetAsync();
511+
User me = await _graphServiceClient.Me.GetAsync();
512512
ViewData["Me"] = me;
513513

514-
var photo = await _graphServiceClient.Me.Photo.Request().GetAsync();
514+
var photo = await _graphServiceClient.Me.Photo.GetAsync();
515515
ViewData["Photo"] = photo;
516516
}
517517
// Catch CAE exception from Graph SDK

0 commit comments

Comments
 (0)