From b8c5193562601a18e2604fb0d8d82e084c43c4c8 Mon Sep 17 00:00:00 2001 From: Chris Sainty Date: Wed, 8 Jul 2020 12:26:33 +0100 Subject: [PATCH] [Blazor][Wasm] Expose login mode option for AAD and AAD B2C Authentication (#23694) * Adds a login mode option via MSAL provider options and updates the AuthenticationService.ts to use the new setting --- .../src/Interop/AuthenticationService.ts | 31 ++++++++++++------- .../src/Models/MsalProviderOptions.cs | 7 ++++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts b/src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts index 03838a5c9b..b06e1892d5 100644 --- a/src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts +++ b/src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts @@ -47,7 +47,8 @@ interface AuthorizeService { interface AuthorizeServiceConfiguration extends Msal.Configuration { defaultAccessTokenScopes: string[]; - additionalScopesToConsent: string[] + additionalScopesToConsent: string[]; + loginMode: string; } class MsalAuthorizeService implements AuthorizeService { @@ -142,18 +143,26 @@ class MsalAuthorizeService implements AuthorizeService { } async signInCore(request: Msal.AuthenticationParameters): Promise { - try { - return await this._msalApplication.loginPopup(request); - } catch (e) { - // If the user explicitly cancelled the pop-up, avoid performing a redirect. - if (this.isMsalError(e) && e.errorCode !== ClientAuthErrorMessage.userCancelledError.code) { - try { - this._msalApplication.loginRedirect(request); - } catch (e) { + if (this._settings.loginMode.toLowerCase() === "redirect") { + try { + this._msalApplication.loginRedirect(request); + } catch (e) { + return e; + } + } else { + try { + return await this._msalApplication.loginPopup(request); + } catch (e) { + // If the user explicitly cancelled the pop-up, avoid performing a redirect. + if (this.isMsalError(e) && e.errorCode !== ClientAuthErrorMessage.userCancelledError.code) { + try { + this._msalApplication.loginRedirect(request); + } catch (e) { + return e; + } + } else { return e; } - } else { - return e; } } } diff --git a/src/Components/WebAssembly/Authentication.Msal/src/Models/MsalProviderOptions.cs b/src/Components/WebAssembly/Authentication.Msal/src/Models/MsalProviderOptions.cs index 8fa6b8b292..c02c651891 100644 --- a/src/Components/WebAssembly/Authentication.Msal/src/Models/MsalProviderOptions.cs +++ b/src/Components/WebAssembly/Authentication.Msal/src/Models/MsalProviderOptions.cs @@ -14,7 +14,7 @@ namespace Microsoft.Authentication.WebAssembly.Msal.Models /// /// Gets or sets the to use for authentication operations. /// - [JsonPropertyName("auth")] + [JsonPropertyName("auth")] public MsalAuthenticationOptions Authentication { get; set; } = new MsalAuthenticationOptions { RedirectUri = "authentication/login-callback", @@ -43,5 +43,10 @@ namespace Microsoft.Authentication.WebAssembly.Msal.Models /// Use this parameter to request consent for scopes for other resources. /// public IList AdditionalScopesToConsent { get; set; } = new List(); + + /// + /// Gets or sets the login mode that is used when initiating the sign-in flow. + /// + public string LoginMode { get; set; } = "popup"; } }