Getting graph client for unattended access

-- -- 872 Reputation points


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.



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

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

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

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

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

`            var authContext = new AuthenticationContext($"{domain}/");`

`            var token = await authContext.AcquireTokenAsync("", 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.
10,569 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 36,891 Reputation points

    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[] { "" };
    // 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,
    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