diff --git a/src/Enable-MsalTokenCacheOnDisk.ps1 b/src/Enable-MsalTokenCacheOnDisk.ps1 index 9a79d78..34abe98 100644 --- a/src/Enable-MsalTokenCacheOnDisk.ps1 +++ b/src/Enable-MsalTokenCacheOnDisk.ps1 @@ -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. @@ -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 @@ -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.' diff --git a/src/internal/TokenCacheHelper.cs b/src/internal/TokenCacheHelper.cs index 44edc4c..e55766c 100644 --- a/src/internal/TokenCacheHelper.cs +++ b/src/internal/TokenCacheHelper.cs @@ -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; } /// /// Path to the token cache /// - 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();