microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

Should OnBehalfOfCredential refresh token automatically?

Open
#2,551 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs: Attention :wave: type:question
Dominant language
C#
Stars
789
Forks
264
Avg merge
15h 17m
Merged PRs (30d)
3

Description

I can't find any documentation for it, the question is if I use OBO flow and initialize graph client like below, should token be automatically refreshed? It does not seem to be or I am missing configuration somewhere. For a long running GraphAPI operation, if token expires, it ends up with: "Assertion is not within its valid time range". Should this work out of the box or if not how should this be implemented? Graph is instantiated only once for my custom graph service class.

OBO flow is used like that:
UI---request+token--->API A---request+token--->API B--->call GraphAPI with OBO token

var scopes = new[] { "https://graph.microsoft.com/.default" };

var options = new OnBehalfOfCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var onBehalfOfCredential = new OnBehalfOfCredential(configuration["AzureAd:TenantId"],
                                                    configuration["AzureAd:ClientId"],
                                                    configuration["AzureAd:ClientSecret"],
                                                    httpContext.HttpContext.Request.Headers.Authorization.ToString()["Bearer ".Length..],
                                                    options);

HttpClientHandler httpClientHandler = new HttpClientHandler();
CustomRetryHandler customRetryHandler = new CustomRetryHandler(httpClientHandler);
var handlers = GraphClientFactory.CreateDefaultHandlers();
var httpClient = GraphClientFactory.Create(handlers);
_graphServiceClient = new GraphServiceClient(httpClient, onBehalfOfCredential, scopes);

What I tried?
Adding "offline_access" to scopes, does not seem to make a difference.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OnBehalfOfCredential and GraphServiceClient entry points shown in the issue, then check the SDK guidance for token caching and renewal in long-running Graph API operations. Document whether the shown setup refreshes tokens automatically, how the credential should be used, and the expected handling when the assertion expires.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, authentication
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.