Error Acquring token for Azure AD B2C on native app (WPF) executable file

Viewed 105

I Have created an native app (WPF) where the authentication system is based on Azure AD B2C. I have been following the documentation here:

Configure authentication in a sample WPF

WPF Sample

And everything works...

But when I create an executable file and install it on another machine the errors begin.

Here is the error message:

Error Acquiring Token:
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.Max[TSource](IEnumerable`1 source)
   at Microsoft.Identity.Client.TokenCache.CalculateSuggestedCacheExpiry()
   at Microsoft.Identity.Client.TokenCache.<Microsoft-Identity-Client-ITokenCacheInternal-SaveTokenResponseAsync>d__59.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.Cache.CacheSessionManager.<SaveTokenResponseAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.Internal.Requests.RequestBase.<CacheTokenResponseAndCreateAuthenticationResultAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.<ExecuteAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.Internal.Requests.RequestBase.<RunAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at active_directory_b2c_wpf.MainWindow.<SignInButton_Click>d__1.MoveNext() in C:\Users\***\Desktop\active-directory-b2c-dotnet-desktop-msalv3\active-directory-b2c-wpf\MainWindow.xaml.cs:line 36 

So what am I doing wrong? And can anybody get the sample app working after they have created an executable file from it?

1 Answers

I had the same problem and managed to fix it: The reason was the cache file which is used, it gives problems if the cache file of another User/PC is read. So in the TokenCacheHelper I do set the username in the filename.

Example:

static TokenCacheHelper()
{
    var userAndDate = $"_{Environment.UserName}_{DateTime.Now:yyyyMM}";  // personalized cache per user, as it fails if it reads the cache of another user
    CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + userAndDate + ".msalcache.bin3";
}

And short time ago it was fixed by Microsoft: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/3130

Related