Skip to content

Commit

Permalink
disable token cache for service principal (#20336)
Browse files Browse the repository at this point in the history
Co-authored-by: NanxiangLiu <[email protected]>
Co-authored-by: Jin Lei <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2022
1 parent fcd4743 commit 44b8085
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
-->

## Upcoming Release
* Enabled caching tokens when logging in with a service principal or client assertion. [#20013]
- This could reduce extra network traffic and improve performance.
- It also fixed the incorrectly short lifespan of tokens.
* Enabled caching tokens when logging in with a client assertion. This fixed the incorrectly short lifespan of tokens.
* Upgraded target framework of Microsoft.Identity.Client to net461 [#20189]
* Stored `ServicePrincipalSecret` and `CertificatePassword` into `AzKeyStore`.
* Updated the reference of Azure PowerShell Common to 1.3.65-preview.
Expand Down
8 changes: 5 additions & 3 deletions src/Accounts/Authenticators/ServicePrincipalAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public override Task<IAccessToken> Authenticate(AuthenticationParameters paramet
var authority = spParameters.Environment.ActiveDirectoryAuthority;

var requestContext = new TokenRequestContext(scopes);
var tokenCachePersistenceOptions = spParameters.TokenCacheProvider.GetTokenCachePersistenceOptions();
// var tokenCachePersistenceOptions = spParameters.TokenCacheProvider.GetTokenCachePersistenceOptions();
AzureSession.Instance.TryGetComponent(nameof(AzureCredentialFactory), out AzureCredentialFactory azureCredentialFactory);

var options = new ClientCertificateCredentialOptions()
{
TokenCachePersistenceOptions = tokenCachePersistenceOptions, // allows MSAL to cache access tokens
// commented due to https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/3218
// todo: investigate splitting user token cache and app token cache
// TokenCachePersistenceOptions = tokenCachePersistenceOptions, // allows MSAL to cache access tokens
AuthorityHost = new Uri(authority),
SendCertificateChain = spParameters.SendCertificateChain ?? default(bool)
};
Expand All @@ -67,7 +69,7 @@ public override Task<IAccessToken> Authenticate(AuthenticationParameters paramet
//Service principal with secret
var csOptions = new ClientSecretCredentialOptions()
{
TokenCachePersistenceOptions = tokenCachePersistenceOptions, // allows MSAL to cache access tokens
// TokenCachePersistenceOptions = tokenCachePersistenceOptions, // allows MSAL to cache access tokens
AuthorityHost = new Uri(authority)
};
tokenCredential = azureCredentialFactory.CreateClientSecretCredential(tenantId, spParameters.ApplicationId, spParameters.Secret, csOptions);
Expand Down

0 comments on commit 44b8085

Please sign in to comment.