microsoft-authentication-extensions-for-dotnet icon indicating copy to clipboard operation
microsoft-authentication-extensions-for-dotnet copied to clipboard

Improved error message to steer users to .WithLinuxUnprotectedFile() or host workaround

Open NeilMacMullen opened this issue 2 years ago • 2 comments

When executing the following code on an application targeting .net 5 and the linux-x64 runtime and running on WSL (Ubuntu)

var storageProperties = new StorageCreationPropertiesBuilder(
        ".msalcache.bin", _directory)
        .WithCacheChangedEvent(_config.ClientId)
       .Build();
_cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties);

I receive this exception:

System.ArgumentNullException: Value cannot be null. (Parameter 'attributeKey1')
   at Microsoft.Identity.Client.Extensions.Msal.LinuxKeyringAccessor..ctor(String cacheFilePath, String keyringCollection, String keyringSchemaName, String keyringSecretLabel, String attributeKey1, String attributeValue1, String attributeKey2, String attributeValue2, TraceSourceLogger logger)
   at Microsoft.Identity.Client.Extensions.Msal.Storage.Create(StorageCreationProperties creationProperties, TraceSource logger)
   at Microsoft.Identity.Client.Extensions.Msal.MsalCacheHelper..ctor(StorageCreationProperties storageCreationProperties, TraceSource logger, HashSet`1 knownAccountIds, FileSystemWatcher cacheWatcher)
   at Microsoft.Identity.Client.Extensions.Msal.MsalCacheHelper.CreateAsync(StorageCreationProperties storageCreationProperties, TraceSource logger)
  

A workaround is to add .WithLinuxUnprotectedFile()

var storageProperties = new StorageCreationPropertiesBuilder(
        ".msalcache.bin", _directory)
        .WithLinuxUnprotectedFile() // bypass secure storage
        .WithCacheChangedEvent(_config.ClientId)
       .Build();

It's also possible there is some piece of Linux magic that would allow the code to work with secure-storage (install lib-secret?)

I would suggest that if possible this scenario should be detected and a more useful exception/message generated. E.g. "No secure storage available: create Storage using "WithLinuxUnprotectedFile" or install lib-secret using "sudu apt-get install....".

NeilMacMullen avatar Sep 22 '21 10:09 NeilMacMullen