Authentication Issues with MSAL.NET on WSL2
Are you having trouble running .NET Core code with Azure AD MSAL Extensions when using WSL or Linux? This post might help you find a workaround.
I’ve found that Microsoft’s Azure AD MSAL Extensions for .NET don’t seem to work very well under WSL2. In particular, there is this this issue that’s reportedly been fixed as of November 2020 but, even with Azure.Identity v1.3.0, it remains a problem for me. The error can look something like value cannot be null: attributeValue1
or Unable to load shared library 'libsecret-1.so.0'
.
I found that I needed 2 fixes:
Wherever you create
new DefaultAzureCredentialOptions
, you should also set the propertyExcludeSharedTokenCacheCredential
totrue
. For example:var credentials = new DefaultAzureCredential ( new DefaultAzureCredentialOptions { ExcludeSharedTokenCacheCredential = SharedUtilities.IsLinuxPlatform(), // ...your other options } );
- In your WSL environment install
libsecret-1-dev
. In Ubuntu for example, run the commandsudo apt install libsecret-1-dev
. This will addlibsecret-1.so.0
to your system so that MSAL can find it.
Alternatively you can roll back to Azure.Identity v1.2.2, which I can confirm was the last version without as many of these issues.
If you’re still having trouble you may also want to check that:
- Azure CLI is properly logged in. On WSL it’s easier to use
az login --use-device-code
and follow the instructions. - The
ASPNETCORE_ENVIRONMENT
environment variable is set toDevelopment
. - You have the appropriate .NET (Core) SDK installed. The latest is 5.0.