在本机应用程序 (WPF) 可执行文件上获取 Azure AD B2C 令牌时出错

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

我创建了一个本机应用程序 (WPF),其中身份验证系统基于 Azure AD B2C。 我一直在关注这里的文档:

Configure authentication in a sample WPF

WPF Sample

一切正常...

但是当我创建一个可执行文件并将其安装在另一台机器上时,错误就开始了。

错误信息如下:

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 

那我做错了什么?在从中创建可执行文件后,任何人都可以让示例应用程序运行吗?

我遇到了同样的问题并设法解决了它:原因是使用的缓存文件,如果读取另一个 User/PC 的缓存文件就会出现问题。所以在 TokenCacheHelper 中我确实在文件名中设置了用户名。

示例:

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";
}

不久前它被微软修复了:https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/3130