Skip to content

Disconnect-MgGraph does not clear the persisted MSAL token cache #3648

Closed

Description

Description

When a user runs Disconnect-MgGraph, the MSAL token cache persisted to disk is not cleared. Currently, LogoutAsync() only:

  1. Clears the in-memory token cache
  2. Nulls out the AuthContext and GraphHttpClient
  3. Deletes the authentication record file (mg.authrecord.json)

The disk-persisted MSAL cache (used when ContextScope == CurrentUser) at %LOCALAPPDATA%\.IdentityService\mg.msal.cache.cae and mg.msal.cache.nocae remains intact. This means cached tokens survive disconnection and can be reused in subsequent sessions without re-authentication.

Expected Behavior

Running Disconnect-MgGraph should fully clear all cached authentication state, including the persisted MSAL token cache on disk, so that subsequent Connect-MgGraph calls require fresh authentication.

Current Behavior

After Disconnect-MgGraph, the persisted token cache files remain on disk. A new PowerShell session can silently reuse cached tokens without prompting for authentication.

Suggested Approach

Azure.Identity does not expose APIs to clear the persisted token cache. However, the MSAL library (Microsoft.Identity.Client.Extensions.Msal) that Azure.Identity depends on can be used directly:

  1. Create StorageCreationProperties matching Azure.Identity's internal cache configuration
  2. Build a MsalCacheHelper targeting the named cache
  3. Call MsalCacheHelper.Clear() to wipe both .cae and .nocae cache variants during disconnect

Environment

  • Module: Microsoft.Graph.Authentication
  • Affected scope: ContextScope.CurrentUser (disk-persisted cache)
  • Platforms: Windows (DPAPI), macOS (Keychain), Linux (libsecret/plaintext)

Activity

  1. added a commit that references this issue on Jul 8, 2026
    f6d24ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions