pnp / pnp/pnpframework

Token not refreshing with AuthenticationManager OBO

Open
#644 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
259
Forks
161
Avg merge
10h 33m
Merged PRs (30d)
1

Description

Hi, I am currently facing a problem using the AuthenticationManager with OnbehalfOf in order to index a Sharepoint site so the task can take several hours and we are currently facing a token refreshing issue.

The AuthenticationManager instance is created as follow:

_authenticationManager = AuthenticationManager.CreateWithOnBehalfOf(clientId,
                                                                                clientSecret,
                                                                                _assertion,
                                                                                tenantId,
                                                                                azureEnvironment);

and the context is retrieved with the following method:

context = _authenticationManager.GetContext(siteUrl);

It appears that the token is never refreshed since after running a task for 70 to 90 minutes I get an exception from MSAL prompting that the token has expired:

Microsoft.Identity.Client.MsalUiRequiredException:AADSTS500133: Assertion is not within its valid time range. Ensure that the access token is not expired before using it for user assertion, or request a new token. Current time: 2022-04-15T07:44:55.1649335Z, expiry time of assertion 2022-04-15T07:44:07.0000000Z.

and with the following stacktrace:

async Task<AuthenticationResult> Microsoft.Identity.Client.Internal.Requests.RequestBase.HandleTokenRefreshErrorAsync(MsalServiceException e, MsalAccessTokenCacheItem cachedAccessTokenItem)|async Task<AuthenticationResult> Microsoft.Identity.Client.Internal.Requests.OnBehalfOfRequest.ExecuteAsync(CancellationToken cancellationToken)|async Task<AuthenticationResult> Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)|async Task<AuthenticationResult> Microsoft.Identity.Client.ApiConfig.Executors.ConfidentialClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenOnBehalfOfParameters onBehalfOfParameters, CancellationToken cancellationToken)|async Task<ClientContext> PnP.Framework.AuthenticationManager.GetContextAsync(string siteUrl, CancellationToken cancellationToken)|ClientContext PnP.Framework.AuthenticationManager.GetContext(string siteUrl)|ClientContext s.cor.MicrosoftGraph.SharePoint.SharePointCsomService.CreateAzureAdAppOnlyAuthenticatedContext(string siteUrl) in SharePointCsomService.cs:99

I haven't found in the framework a way to refresh the token.

Is this a bug? If not, I haven't found any information to help me with that situation if this is working as intended intended.

I am using version 1.9.0

Thanks for your help :)

Contributor guide

No contributing guide indexed for this repository

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 AuthenticationManager.CreateWithOnBehalfOf and GetContextAsync, following the call from SharePointCsomService.cs:99. Reproduce the long-running flow on version 1.9.0 and inspect how the MSAL OnBehalfOf assertion is handled when the token expires. Done means determining whether the token can be refreshed or documenting the intended behavior and required usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
authentication
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.