diff --git a/CommunityToolkit.Authentication.Msal/MsalProvider.cs b/CommunityToolkit.Authentication.Msal/MsalProvider.cs index 3adf77a..687acfc 100644 --- a/CommunityToolkit.Authentication.Msal/MsalProvider.cs +++ b/CommunityToolkit.Authentication.Msal/MsalProvider.cs @@ -7,6 +7,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; using Microsoft.Graph; @@ -106,7 +107,11 @@ optionsMiddleware is AuthenticationHandlerOption options && options.AuthenticationProviderOption?.Scopes != null && options.AuthenticationProviderOption.Scopes.Length > 0) { var withScopes = options.AuthenticationProviderOption.Scopes; - token = await this.GetTokenWithScopesAsync(withScopes); + token = await this.GetTokenWithScopesAsync(withScopes, out var exception); + if (exception != null) + { + ExceptionDispatchInfo.Capture(exception).Throw(); + } } else { @@ -170,10 +175,15 @@ public override async Task SignOutAsync() State = ProviderState.SignedOut; } + public Task GetTokenAsync(out Exception exception, bool silentOnly = false) + { + return this.GetTokenWithScopesAsync(Scopes, silentOnly, out exception); + } + /// public override Task GetTokenAsync(bool silentOnly = false) { - return this.GetTokenWithScopesAsync(Scopes, silentOnly); + return this.GetTokenWithScopesAsync(Scopes, silentOnly, out _); } /// @@ -221,7 +231,7 @@ protected IPublicClientApplication CreatePublicClientApplication(string clientId /// An array of scopes to pass along with the Graph request. /// A value to determine whether account broker UI should be shown, if required by MSAL. /// A representing the result of the asynchronous operation. - protected async Task GetTokenWithScopesAsync(string[] scopes, bool silentOnly = false) + protected async Task GetTokenWithScopesAsync(string[] scopes, bool silentOnly = false, out Exception exception) { await SemaphoreSlim.WaitAsync(); @@ -239,10 +249,9 @@ protected async Task GetTokenWithScopesAsync(string[] scopes, bool silen catch (MsalUiRequiredException) { } - catch + catch (Exception e) { - // Unexpected exception - // TODO: Send exception to a logger. + exception = e; } if (authResult == null && !silentOnly) @@ -269,11 +278,11 @@ protected async Task GetTokenWithScopesAsync(string[] scopes, bool silen #endif authResult = await paramBuilder.ExecuteAsync(); + exception = null; // if we succeeded in a retry, clear the exception. } - catch + catch (Exception e) { - // Unexpected exception - // TODO: Send exception to a logger. + exception = e; } }