Skip to content

Commit d0ed1ab

Browse files
authored
Merge pull request #106107 from waldekmastykarz/uwp-graph-sdkv5
Updates the UWP tutorial to use Microsoft Graph .NET SDK v5
2 parents 1f49e1d + fdd7e99 commit d0ed1ab

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

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

Lines changed: 50 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,42 @@ 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*: right-click on the project, select **Add** > **New Item** > **Blank Page**.
241+
242+
Add to the newly created file the following code:
243+
244+
```csharp
245+
using Microsoft.Kiota.Abstractions.Authentication;
246+
using System;
247+
using System.Collections.Generic;
248+
using System.Threading;
249+
using System.Threading.Tasks;
250+
251+
namespace UWP_app_MSGraph {
252+
public class TokenProvider : IAccessTokenProvider {
253+
private Func<string[], Task<string>> getTokenDelegate;
254+
private string[] scopes;
255+
256+
public TokenProvider(Func<string[], Task<string>> getTokenDelegate, string[] scopes) {
257+
this.getTokenDelegate = getTokenDelegate;
258+
this.scopes = scopes;
259+
}
260+
261+
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = default,
262+
CancellationToken cancellationToken = default) {
263+
return getTokenDelegate(scopes);
264+
}
265+
266+
public AllowedHostsValidator AllowedHostsValidator { get; }
267+
}
268+
}
269+
```
270+
271+
> [!TIP]
272+
> After pasting the code, make sure that the namespace in the *TokenProvider.cs* file matches the namespace of your project. This will allow you to more easily reference the `TokenProvider` class in your project.
273+
274+
The `TokenProvider` class defines a custom access token provider that executes the specified delegate method to get and return an access token.
275+
239276
Add the following new method to *MainPage.xaml.cs*:
240277

241278
```csharp
@@ -245,16 +282,22 @@ Add the following new method to *MainPage.xaml.cs*:
245282
/// <returns>GraphServiceClient</returns>
246283
private async static Task<GraphServiceClient> SignInAndInitializeGraphServiceClient(string[] scopes)
247284
{
248-
GraphServiceClient graphClient = new GraphServiceClient(MSGraphURL,
249-
new DelegateAuthenticationProvider(async (requestMessage) =>
250-
{
251-
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", await SignInUserAndGetTokenUsingMSAL(scopes));
252-
}));
285+
var tokenProvider = new TokenProvider(SignInUserAndGetTokenUsingMSAL, scopes);
286+
var authProvider = new BaseBearerTokenAuthenticationProvider(tokenProvider);
287+
var graphClient = new GraphServiceClient(authProvider, MSGraphURL);
253288

254289
return await Task.FromResult(graphClient);
255290
}
256291
```
257292

293+
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.
294+
295+
To use the `BaseBearerTokenAuthenticationProvider`, in the *MainPage.xaml.cs* file, add the following reference:
296+
297+
```cs
298+
using Microsoft.Kiota.Abstractions.Authentication;
299+
```
300+
258301
#### More information on making a REST call against a protected API
259302

260303
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)