Christopher King
Christopher King

Reputation: 1062

How do I authenticate to Microsoft.Graph using the Beta client?

I'd like to call the CloudPC rest API using a C# client. The C# client is in the beta nuget package Microsoft.Graph.Beta. Here is a sample client that illustrates how to authenticate using the the non-beta client.

// Build a client application.
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create("INSERT-CLIENT-APP-ID")
            .Build();
// Create an authentication provider by passing in a client application and graph scopes.
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, graphScopes);
// Create a new instance of GraphServiceClient with the authentication provider.
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

When I try to use that sample client with the non-beta nuget I get discover I cannot resolve DeviceCodeProvider. So what I can I use instead of DeviceCodeProvider?

Michael Mainer tells me I can use DeviceCodeCredential which does resolve. Still the sample is not yet functional. So now I'm trying to adapt this sample Michael sent me.

Michael also sent me a pointer to LinqPad samples which are useful for poking at the API.

I have a bunch of queries setup MIchaelMainer/graph-test-harness: This repository contains samples for a Microsoft Graph test harness to quickly and easily test SDKs (github.com) to help try out the libraries. I have an option for DeviceCodeCredential.

Upvotes: 2

Views: 1986

Answers (1)

Michael Mainer
Michael Mainer

Reputation: 3475

That is in the Microsoft.Graph.Auth package, which will be deprecated soon. If you use the preview version of Microsoft.Graph.Beta, which takes a dependency on preview Microsoft.Graph.Core, you'll be able to use the Azure.Identity DeviceCodeCredential which is the equivalent of DeviceCodeProvider and is the future of auth in the graph SDK.

https://github.com/andrueastman/TokenCrendentialAuthProvider/blob/master/TokenCredentialSample/Program.cs

The preview version of Microsoft.Graph (4.x.x-preview), Microsoft.Graph.Beta (4.x.x-preview), and Microsoft.Graph.Core (2.x.x-preview) supports Azure.Identity TokenCredential. This will allow you to use the same auth library for both Azure and Microsoft Graph development work.

using Microsoft.Graph;
using Azure.Identity;

DeviceCodeCredentialOptions options = new()
{
    ClientId = "public_clientid",
    TenantId = "tenantId"
};

GraphServiceClient client = new (new DeviceCodeCredential(options));

Upvotes: 4

Related Questions