Skip to content

Commit 20a187d

Browse files
Updates the UWP tutorial to use Microsoft Graph .NET SDK v5
1 parent 839853a commit 20a187d

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

articles/active-directory/develop/tutorial-v2-windows-uwp.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: tutorial
1111
ms.workload: identity
12-
ms.date: 12/13/2019
12+
ms.date: 03/03/2023
1313
ms.author: henrymbugua
1414
ms.reviewer: jmprieur
1515
ms.custom: "devx-track-csharp, aaddev, identityplatformtop40"
@@ -108,6 +108,7 @@ This section shows how to use the Microsoft Authentication Library to get a toke
108108
```csharp
109109
using Microsoft.Identity.Client;
110110
using Microsoft.Graph;
111+
using Microsoft.Graph.Models;
111112
using System.Diagnostics;
112113
using System.Threading.Tasks;
113114
using System.Net.Http.Headers;
@@ -152,7 +153,7 @@ This section shows how to use the Microsoft Authentication Library to get a toke
152153
GraphServiceClient graphClient = await SignInAndInitializeGraphServiceClient(scopes);
153154

154155
// Call the /me endpoint of Graph
155-
User graphUser = await graphClient.Me.Request().GetAsync();
156+
User graphUser = await graphClient.Me.GetAsync();
156157

157158
// Go back to the UI thread to make changes to the UI
158159
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
@@ -236,6 +237,37 @@ Eventually, the `AcquireTokenSilent` method fails. Reasons for failure include a
236237

237238
### Instantiate the Microsoft Graph Service Client by obtaining the token from the SignInUserAndGetTokenUsingMSAL method
238239

240+
In the project, create a new file named *TokenProvider.cs* with the following code:
241+
242+
```csharp
243+
using Microsoft.Kiota.Abstractions.Authentication;
244+
using System;
245+
using System.Collections.Generic;
246+
using System.Threading;
247+
using System.Threading.Tasks;
248+
249+
namespace UWP_app_MSGraph {
250+
public class TokenProvider : IAccessTokenProvider {
251+
private Func<string[], Task<string>> getTokenDelegate;
252+
private string[] scopes;
253+
254+
public TokenProvider(Func<string[], Task<string>> getTokenDelegate, string[] scopes) {
255+
this.getTokenDelegate = getTokenDelegate;
256+
this.scopes = scopes;
257+
}
258+
259+
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
260+
CancellationToken cancellationToken = default) {
261+
return getTokenDelegate(scopes);
262+
}
263+
264+
public AllowedHostsValidator AllowedHostsValidator { get; }
265+
}
266+
}
267+
```
268+
269+
This class defines a custom access token provider that executes the specified delegate method to get and and return an access token.
270+
239271
Add the following new method to *MainPage.xaml.cs*:
240272

241273
```csharp
@@ -245,16 +277,16 @@ Add the following new method to *MainPage.xaml.cs*:
245277
/// <returns>GraphServiceClient</returns>
246278
private async static Task<GraphServiceClient> SignInAndInitializeGraphServiceClient(string[] scopes)
247279
{
248-
GraphServiceClient graphClient = new GraphServiceClient(MSGraphURL,
249-
new DelegateAuthenticationProvider(async (requestMessage) =>
250-
{
251-
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", await SignInUserAndGetTokenUsingMSAL(scopes));
252-
}));
280+
var tokenProvider = new TokenProvider(SignInUserAndGetTokenUsingMSAL, scopes);
281+
var authProvider = new BaseBearerTokenAuthenticationProvider(tokenProvider);
282+
var graphClient = new GraphServiceClient(authProvider, MSGraphURL);
253283

254284
return await Task.FromResult(graphClient);
255285
}
256286
```
257287

288+
In this method, you're using the custom access token provider `TokenProvider` to connect the `SignInUserAndGetTokenUsingMSAL` method to the Microsoft Graph .NET SDK and create an authenticated client.
289+
258290
#### More information on making a REST call against a protected API
259291

260292
In this sample application, the `GetGraphServiceClient` method instantiates `GraphServiceClient` by using an access token. Then, `GraphServiceClient` is used to get the user's profile information from the **me** endpoint.

0 commit comments

Comments
 (0)