Skip to content

Commit b8c5193

Browse files
authored
[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
1 parent 37a4457 commit b8c5193

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ interface AuthorizeService {
4747

4848
interface AuthorizeServiceConfiguration extends Msal.Configuration {
4949
defaultAccessTokenScopes: string[];
50-
additionalScopesToConsent: string[]
50+
additionalScopesToConsent: string[];
51+
loginMode: string;
5152
}
5253

5354
class MsalAuthorizeService implements AuthorizeService {
@@ -142,18 +143,26 @@ class MsalAuthorizeService implements AuthorizeService {
142143
}
143144

144145
async signInCore(request: Msal.AuthenticationParameters): Promise<Msal.AuthResponse | Msal.AuthError | undefined> {
145-
try {
146-
return await this._msalApplication.loginPopup(request);
147-
} catch (e) {
148-
// If the user explicitly cancelled the pop-up, avoid performing a redirect.
149-
if (this.isMsalError(e) && e.errorCode !== ClientAuthErrorMessage.userCancelledError.code) {
150-
try {
151-
this._msalApplication.loginRedirect(request);
152-
} catch (e) {
146+
if (this._settings.loginMode.toLowerCase() === "redirect") {
147+
try {
148+
this._msalApplication.loginRedirect(request);
149+
} catch (e) {
150+
return e;
151+
}
152+
} else {
153+
try {
154+
return await this._msalApplication.loginPopup(request);
155+
} catch (e) {
156+
// If the user explicitly cancelled the pop-up, avoid performing a redirect.
157+
if (this.isMsalError(e) && e.errorCode !== ClientAuthErrorMessage.userCancelledError.code) {
158+
try {
159+
this._msalApplication.loginRedirect(request);
160+
} catch (e) {
161+
return e;
162+
}
163+
} else {
153164
return e;
154165
}
155-
} else {
156-
return e;
157166
}
158167
}
159168
}

src/Components/WebAssembly/Authentication.Msal/src/Models/MsalProviderOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class MsalProviderOptions
1414
/// <summary>
1515
/// Gets or sets the <see cref="MsalAuthenticationOptions"/> to use for authentication operations.
1616
/// </summary>
17-
[JsonPropertyName("auth")]
17+
[JsonPropertyName("auth")]
1818
public MsalAuthenticationOptions Authentication { get; set; } = new MsalAuthenticationOptions
1919
{
2020
RedirectUri = "authentication/login-callback",
@@ -43,5 +43,10 @@ public class MsalProviderOptions
4343
/// Use this parameter to request consent for scopes for other resources.
4444
/// </remarks>
4545
public IList<string> AdditionalScopesToConsent { get; set; } = new List<string>();
46+
47+
/// <summary>
48+
/// Gets or sets the login mode that is used when initiating the sign-in flow.
49+
/// </summary>
50+
public string LoginMode { get; set; } = "popup";
4651
}
4752
}

0 commit comments

Comments
 (0)