Compile Error with DelegateAuthenticationProvider

Kenta Matsunaga 60 Reputation points
2024-01-24T01:30:49.6666667+00:00

Hello, I would like to write code in C# to create a Microsoft Graph client from a refreshToken. However, when I used DelegateAuthenticationProvider, I encountered a CS0246 error. Error: The type or namespace name 'DelegateAuthenticationProvider' could not be found (are you missing a using directive or an assembly reference?)

How can this be resolved?
The versions of my packages are as follows:
Microsoft.Graph: 5.39.0
Microsoft.Identity.Client: 4.10.0

Microsoft Security | Microsoft Graph
0 comments No comments

Answer accepted by question author
CarlZhao-MSFT 46,456 Reputation points
2024-01-24T02:52:46.2333333+00:00

Hi @Kenta Matsunaga

The DelegateAuthenticationProvider class is no longer available in Microsoft.Graph version 5.0.0 or later. This is because the Graph SDK has changed the way it handles authentication and now uses the Azure.Identity package. https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md#authentication

To fix this error, you need to use a different class that implements the IAccessTokenProvider interface from the Azure.Identity package, such as OnBehalfOfCredential. Then, you can pass this class to the GraphServiceClient constructor along with the scopes you need.

using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;


string refreshToken = "...";

string[] scopes = new[] { "User.Read" };

var onBehalfOfCredential = new OnBehalfOfCredential(
    tenantId: "your-tenant-id",
    clientId: "your-client-id",
    clientSecret: "your-client-secret",
    userAssertionToken: refreshToken
);

var graphClient = new GraphServiceClient(onBehalfOfCredential, scopes);

Hope this helps.

If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.