From 228703527ad9548c71df89bcc7029c4cd09f78fa Mon Sep 17 00:00:00 2001 From: Jezour1sw <93341931+Jezour1sw@users.noreply.github.com> Date: Sat, 1 Apr 2023 00:59:09 +0200 Subject: [PATCH 1/3] Update TokenCacheHelper.cs it should allow using the caching for more than one username/password via AAD. --- src/internal/TokenCacheHelper.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/internal/TokenCacheHelper.cs b/src/internal/TokenCacheHelper.cs index 44edc4c..edd71bb 100644 --- a/src/internal/TokenCacheHelper.cs +++ b/src/internal/TokenCacheHelper.cs @@ -5,16 +5,19 @@ public static class TokenCacheHelper { - public static void EnableSerialization(ITokenCache tokenCache) + public static void EnableSerialization(ITokenCache tokenCache, string username = "") { tokenCache.SetBeforeAccess(BeforeAccessNotification); tokenCache.SetAfterAccess(AfterAccessNotification); + Username = username; } /// /// Path to the token cache /// - public static readonly string CacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSAL.PS", "MSAL.PS.msalcache.bin3"); + private static readonly string Username = ""; + + public static readonly string CacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSAL.PS", Username + "MSAL.PS.msalcache.bin3"); private static readonly object FileLock = new object(); From ed8d1c6d8ff1bf8b8723a18da043089504242efc Mon Sep 17 00:00:00 2001 From: Jezour1sw <93341931+Jezour1sw@users.noreply.github.com> Date: Sat, 1 Apr 2023 01:06:31 +0200 Subject: [PATCH 2/3] Update Enable-MsalTokenCacheOnDisk.ps1 Enable to define own path to the cache file --- src/Enable-MsalTokenCacheOnDisk.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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.' From 14d27f28e55ff0dda23be0e251cb312003d6410d Mon Sep 17 00:00:00 2001 From: Jezour1sw <93341931+Jezour1sw@users.noreply.github.com> Date: Sat, 1 Apr 2023 01:12:06 +0200 Subject: [PATCH 3/3] Update TokenCacheHelper.cs Enable define own path for storing the cached token --- src/internal/TokenCacheHelper.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/internal/TokenCacheHelper.cs b/src/internal/TokenCacheHelper.cs index edd71bb..e55766c 100644 --- a/src/internal/TokenCacheHelper.cs +++ b/src/internal/TokenCacheHelper.cs @@ -5,19 +5,21 @@ public static class TokenCacheHelper { - public static void EnableSerialization(ITokenCache tokenCache, string username = "") + public static void EnableSerialization(ITokenCache tokenCache, string cacheFilePath = "") { tokenCache.SetBeforeAccess(BeforeAccessNotification); tokenCache.SetAfterAccess(AfterAccessNotification); - Username = username; + 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 /// - private static readonly string Username = ""; - public static readonly string CacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSAL.PS", Username + "MSAL.PS.msalcache.bin3"); + public static readonly string CacheFilePath; private static readonly object FileLock = new object();