首页 > 解决方案 > 在 Mono for MacOS 上使用 MSAL 检索授权令牌

问题描述

我正在尝试使用 MSAL 库从 MacOS 上的桌面 .net 应用程序检索 Azure AD B2C 身份验证令牌以调用安全 API。这在构建时工作得很好dotnetcore

var app = PublicClientApplicationBuilder.Create(ClientId)
                .WithB2CAuthority(AuthoritySignUpSignIn)
                .WithRedirectUri(RedirectUrl)
                .Build();

IEnumerable<IAccount> accounts = await app.GetAccountsAsync(PolicySignUpSignIn);

AuthenticationResult result = await app.AcquireTokenInteractive(ApiScopes)
                .WithUseEmbeddedWebView(false)
                .WithAccount(accounts.FirstOrDefault())
                .ExecuteAsync();

但是,由于此应用程序将作为嵌入式插件运行,因此我需要针对 .net 框架目标进行构建。运行针对目标构建并在 Mono for Mac 上执行的相同代码net48会发生一些异常(见下文)。

System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.Identity.Client.PlatformsCommon.Shared.DesktopOsHelper.GetWindowsVersionStringInternal () [0x0000f] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.PlatformsCommon.Shared.DesktopOsHelper+<>c.<.cctor>b__18_2 () [0x00000] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) [0x00022] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].CreateValue () [0x00074] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at Microsoft.Identity.Client.PlatformsCommon.Shared.DesktopOsHelper.GetWindowsVersionString () [0x00000] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Platforms.net45.NetDesktopPlatformProxy.InternalGetOperatingSystem () [0x00000] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) [0x00022] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].CreateValue () [0x00074] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <3a5ca7554402454690eb498ecf91eca6>:0 
  at Microsoft.Identity.Client.PlatformsCommon.Shared.AbstractPlatformProxy.GetOperatingSystem () [0x00000] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.MsalIdHelper.GetMsalIdParameters (Microsoft.Identity.Client.Core.ICoreLogger logger) [0x00057] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.CreateAuthorizationRequestParameters (System.Uri redirectUriOverride) [0x00223] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.CreateAuthorizationUri (System.Boolean addPkceAndState) [0x00000] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.FetchAuthCodeAndPkceInternalAsync (Microsoft.Identity.Client.UI.IWebUI webUi, System.Threading.CancellationToken cancellationToken) [0x00046] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.FetchAuthCodeAndPkceVerifierAsync (System.Threading.CancellationToken cancellationToken) [0x0007d] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.GetTokenResponseAsync (System.Threading.CancellationToken cancellationToken) [0x00182] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.ExecuteAsync (System.Threading.CancellationToken cancellationToken) [0x000fa] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync (System.Threading.CancellationToken cancellationToken) [0x001e2] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.ExecuteAsync (Microsoft.Identity.Client.ApiConfig.Parameters.AcquireTokenCommonParameters commonParameters, Microsoft.Identity.Client.ApiConfig.Parameters.AcquireTokenInteractiveParameters interactiveParameters, System.Threading.CancellationToken cancellationToken) [0x00142] in <b9a99487e27c4dd08624b5deef1ad993>:0 
  at up_console.Program.RunAsync () [0x00142] in <e5a2c54d28494d56b9935be85f2b417a>:0 
  at up_console.Program.Main (System.String[] args) [0x0000d] in <e5a2c54d28494d56b9935be85f2b417a>:0 

我也尝试过使用CustomWebUi,但这并没有改变任何东西。

阅读官方 MSAL.net 要求,我了解 MacOS 上的 Mono 似乎不受支持,但是在一些较旧的发行说明中提到了 Mono:

在某些情况下,也可以在运行时使用 .NET 标准 1.3 程序集。也就是说,例如,如果您正在构建的应用程序是针对 MSAL.NET 不明确支持的平台的情况。此类平台的示例包括 Xamarin.Mac、Unity、Mono或旧框架,例如 Windows Phone、Windows Phone Silverlight。

关于如何让 MSAL.net 在 Mono for Mac 上运行的任何想法?

标签: c#.netmacosmonomsal

解决方案


推荐阅读