Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Enable-MsalTokenCacheOnDisk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
.EXAMPLE
PS C:\>Enable-MsalTokenCacheOnDisk $ClientApplication
Enable client application to use persistent token cache on disk.
.EXAMPLE
PS C:\>Enable-MsalTokenCacheOnDisk $ClientApplication -CacheFilePath $CacheFilePath
Enable client application to use persistent token cache on disk.
.EXAMPLE
PS C:\>Enable-MsalTokenCacheOnDisk $ClientApplication -PassThru
Enable client application to use persistent token cache on disk and return the object.
Expand All @@ -21,6 +24,9 @@ function Enable-MsalTokenCacheOnDisk {
# Confidential client application
[Parameter(Mandatory = $true, ParameterSetName = 'ConfidentialClient', Position = 0, ValueFromPipeline = $true)]
[Microsoft.Identity.Client.IConfidentialClientApplication] $ConfidentialClientApplication,
# Path to Cache File
[Parameter(Mandatory = $false)]
[string] $CacheFilePath,
# Returns client application
[Parameter(Mandatory = $false)]
[switch] $PassThru
Expand All @@ -39,9 +45,9 @@ function Enable-MsalTokenCacheOnDisk {

if ([System.Environment]::OSVersion.Platform -eq 'Win32NT' -and $PSVersionTable.PSVersion -lt [version]'6.0') {
if ($ClientApplication -is [Microsoft.Identity.Client.IConfidentialClientApplication]) {
[TokenCacheHelper]::EnableSerialization($ClientApplication.AppTokenCache)
[TokenCacheHelper]::EnableSerialization($ClientApplication.AppTokenCache, $CacheFilePath)
}
[TokenCacheHelper]::EnableSerialization($ClientApplication.UserTokenCache)
[TokenCacheHelper]::EnableSerialization($ClientApplication.UserTokenCache, $CacheFilePath)
}
else {
Write-Warning 'Using TokenCache On Disk only works on Windows platform using Windows PowerShell. The token cache will stored in memory and not persisted on disk.'
Expand Down
9 changes: 7 additions & 2 deletions src/internal/TokenCacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@

public static class TokenCacheHelper
{
public static void EnableSerialization(ITokenCache tokenCache)
public static void EnableSerialization(ITokenCache tokenCache, string cacheFilePath = "")
{
tokenCache.SetBeforeAccess(BeforeAccessNotification);
tokenCache.SetAfterAccess(AfterAccessNotification);
if(string.IsNullOrEmpty(CacheFilePath))
CacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSAL.PS", "MSAL.PS.msalcache.bin3");
else
CacheFilePath = cacheFilePath;
}

/// <summary>
/// Path to the token cache
/// </summary>
public static readonly string CacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSAL.PS", "MSAL.PS.msalcache.bin3");

public static readonly string CacheFilePath;

private static readonly object FileLock = new object();

Expand Down