Getting graph client for unattended access

-- -- 892 Reputation points
2024-03-31T13:54:05.1366667+00:00

Hi

I was previously using below code at the end to get graph client for unattended access. After updating to latest MS Graph I am getting the error

The type or namespace name 'DelegateAuthenticationProvider' could not be found

How can I get the unattended graph client in lates MS Graph? A short code snippet would be great.

Thanks

Regards

    `private static async Task<GraphServiceClient> GetGraphApiClient()`
`        {`

`            var clientId = "-----------------";`

`            var secret = "------------";`

`            var domain = "---------------";`

`            var credentials = new ClientCredential(clientId, secret);`

`            var authContext = new AuthenticationContext($"https://login.microsoftonline.com/{domain}/");`

`            var token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);`

`            var accessToken = token.AccessToken;`

`            var graphServiceClient = new GraphServiceClient(`

`                new DelegateAuthenticationProvider((requestMessage) =>`

`                {`

`                    requestMessage`

`                .Headers`

`                .Authorization = new AuthenticationHeaderValue("bearer", accessToken);`

`                    return Task.CompletedTask;`

`                }));`

`            return graphServiceClient;`

`        }`

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,461 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 40,311 Reputation points
    2024-04-01T02:47:33.56+00:00

    Hi @-- --

    The DelegateAuthenticationProvider class has been deprecated in the latest versions of the Microsoft Graph SDK. Instead, you can use the new authentication approach using Azure.Identity.

    Furthermore, even before Graph SDK V5.0, the DelegateAuthenticationProvider class was not used to obtain the graph client for unattended access, it was a delegated class and the user had to participate in the login.

    If you wish to create an unattended Graph client, please refer to the following code snippet:

    using Microsoft.Graph;
    using Azure.Identity;
    // ...
    private static async Task<GraphServiceClient> GetGraphApiClient()
    {
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    // Values from app registration
    var clientId = "YOUR_CLIENT_ID";
    var tenantId = "YOUR_TENANT_ID";
    var clientSecret = "YOUR_CLIENT_SECRET";
    // using Azure.Identity;
    var options = new ClientSecretCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    var graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);
    return graphServiceClient;
    }
    

    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.

    0 comments No comments