diff --git a/src/Components/WebAssembly/Authentication.Msal/src/MsalWebAssemblyServiceCollectionExtensions.cs b/src/Components/WebAssembly/Authentication.Msal/src/MsalWebAssemblyServiceCollectionExtensions.cs
index 7217c196c6..9320f7c748 100644
--- a/src/Components/WebAssembly/Authentication.Msal/src/MsalWebAssemblyServiceCollectionExtensions.cs
+++ b/src/Components/WebAssembly/Authentication.Msal/src/MsalWebAssemblyServiceCollectionExtensions.cs
@@ -22,7 +22,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// The .
/// A callback to configure the .
/// The .
- public static IServiceCollection AddMsalAuthentication(this IServiceCollection services, Action> configure)
+ public static IRemoteAuthenticationBuilder AddMsalAuthentication(this IServiceCollection services, Action> configure)
{
return AddMsalAuthentication(services, configure);
}
@@ -34,11 +34,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// The .
/// A callback to configure the .
/// The .
- public static IServiceCollection AddMsalAuthentication(this IServiceCollection services, Action> configure)
+ public static IRemoteAuthenticationBuilder AddMsalAuthentication(this IServiceCollection services, Action> configure)
where TRemoteAuthenticationState : RemoteAuthenticationState, new()
{
- AddMsalAuthentication(services, configure);
- return services;
+ return AddMsalAuthentication(services, configure);
}
///
@@ -49,14 +48,24 @@ namespace Microsoft.Extensions.DependencyInjection
/// The .
/// A callback to configure the .
/// The .
- public static IServiceCollection AddMsalAuthentication(this IServiceCollection services, Action> configure)
+ public static IRemoteAuthenticationBuilder AddMsalAuthentication(this IServiceCollection services, Action> configure)
where TRemoteAuthenticationState : RemoteAuthenticationState, new()
where TAccount : RemoteUserAccount
{
- services.AddRemoteAuthentication(configure);
+ services.AddRemoteAuthentication(configure);
services.TryAddEnumerable(ServiceDescriptor.Singleton>, MsalDefaultOptionsConfiguration>());
- return services;
+ return new MsalRemoteAuthenticationBuilder(services);
}
}
+
+ internal class MsalRemoteAuthenticationBuilder : IRemoteAuthenticationBuilder
+ where TRemoteAuthenticationState : RemoteAuthenticationState, new()
+ where TRemoteUserAccount : RemoteUserAccount
+ {
+
+ public MsalRemoteAuthenticationBuilder(IServiceCollection services) => Services = services;
+
+ public IServiceCollection Services { get; }
+ }
}
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts
index cb1b34ae52..67b9bf6b6f 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts
@@ -332,8 +332,8 @@ export class AuthenticationService {
return AuthenticationService.instance.getUser();
}
- public static getAccessToken() {
- return AuthenticationService.instance.getAccessToken();
+ public static getAccessToken(options: AccessTokenRequestOptions) {
+ return AuthenticationService.instance.getAccessToken(options);
}
public static signIn(state: unknown) {
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationBuilderExtensions.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationBuilderExtensions.cs
index 9040a726a7..29645fc671 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationBuilderExtensions.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationBuilderExtensions.cs
@@ -23,9 +23,9 @@ namespace Microsoft.Extensions.DependencyInjection
this IRemoteAuthenticationBuilder builder)
where TRemoteAuthenticationState : RemoteAuthenticationState, new()
where TAccount : RemoteUserAccount
- where TUserFactory : UserFactory
+ where TUserFactory : AccountClaimsPrincipalFactory
{
- builder.Services.Replace(ServiceDescriptor.Scoped, TUserFactory>());
+ builder.Services.Replace(ServiceDescriptor.Scoped, TUserFactory>());
return builder;
}
@@ -40,7 +40,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static IRemoteAuthenticationBuilder AddUserFactory(
this IRemoteAuthenticationBuilder builder)
where TRemoteAuthenticationState : RemoteAuthenticationState, new()
- where TUserFactory : UserFactory => builder.AddUserFactory();
+ where TUserFactory : AccountClaimsPrincipalFactory => builder.AddUserFactory();
///
/// Replaces the existing with the user factory defined by .
@@ -50,6 +50,6 @@ namespace Microsoft.Extensions.DependencyInjection
/// The .
public static IRemoteAuthenticationBuilder AddUserFactory(
this IRemoteAuthenticationBuilder builder)
- where TUserFactory : UserFactory => builder.AddUserFactory();
+ where TUserFactory : AccountClaimsPrincipalFactory => builder.AddUserFactory();
}
}
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs
index 35aaba6e02..47582a2b7a 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs
@@ -177,7 +177,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
await ProcessLogInCallback();
break;
case RemoteAuthenticationActions.LogInFailed:
- _message = GetErrorMessage();
break;
case RemoteAuthenticationActions.Profile:
if (ApplicationPaths.RemoteProfilePath == null)
@@ -209,7 +208,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
await ProcessLogOutCallback();
break;
case RemoteAuthenticationActions.LogOutFailed:
- _message = GetErrorMessage();
break;
case RemoteAuthenticationActions.LogOutSucceeded:
break;
@@ -235,8 +233,8 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
await NavigateToReturnUrl(GetReturnUrl(result.State, returnUrl));
break;
case RemoteAuthenticationStatus.Failure:
- var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogInFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
- await NavigateToReturnUrl(uri);
+ _message = result.ErrorMessage;
+ Navigation.NavigateTo(ApplicationPaths.LogInFailedPath);
break;
case RemoteAuthenticationStatus.OperationCompleted:
default:
@@ -297,8 +295,8 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
case RemoteAuthenticationStatus.OperationCompleted:
break;
case RemoteAuthenticationStatus.Failure:
- var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogOutFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
- await NavigateToReturnUrl(uri);
+ _message = result.ErrorMessage;
+ Navigation.NavigateTo(ApplicationPaths.LogOutFailedPath);
break;
default:
throw new InvalidOperationException($"Invalid authentication result status.");
@@ -363,8 +361,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
private ValueTask RedirectToProfile() => JS.InvokeVoidAsync("location.replace", Navigation.ToAbsoluteUri(ApplicationPaths.RemoteProfilePath).PathAndQuery);
- private string GetErrorMessage() => QueryStringHelper.GetParameter(new Uri(Navigation.Uri).Query, "message");
-
private static void DefaultLogInFragment(RenderTreeBuilder builder)
{
builder.OpenElement(0, "p");
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs
index 387adb3aaa..db1bd6faf9 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs
@@ -10,18 +10,21 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
///
public class AccessTokenResult
{
- private AccessToken _token;
+ private readonly AccessToken _token;
+ private readonly NavigationManager _navigation;
///
/// Initializes a new instance of .
///
/// The status of the result.
/// The in case it was successful.
+ /// The to perform redirects.
/// The redirect uri to go to for provisioning the token.
- public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, string redirectUrl)
+ public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, NavigationManager navigation, string redirectUrl)
{
Status = status;
_token = token;
+ _navigation = navigation;
RedirectUrl = redirectUrl;
}
@@ -53,5 +56,27 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
return false;
}
}
+
+ ///
+ /// Determines whether the token request was successful and makes the available for use when it is.
+ ///
+ /// The if the request was successful.
+ /// Whether or not to redirect automatically when failing to provision a token.
+ /// true when the token request is successful; false otherwise.
+ public bool TryGetToken(out AccessToken accessToken, bool redirect)
+ {
+ if (TryGetToken(out accessToken))
+ {
+ return true;
+ }
+ else
+ {
+ if (redirect)
+ {
+ _navigation.NavigateTo(RedirectUrl);
+ }
+ return false;
+ }
+ }
}
}
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/UserFactory.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccountClaimsPrincipalFactory.cs
similarity index 92%
rename from src/Components/WebAssembly/WebAssembly.Authentication/src/Services/UserFactory.cs
rename to src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccountClaimsPrincipalFactory.cs
index 1c7aa980fa..d81a802fa0 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/UserFactory.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccountClaimsPrincipalFactory.cs
@@ -12,12 +12,12 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
/// Converts into a .
///
/// The account type.
- public class UserFactory where TAccount : RemoteUserAccount
+ public class AccountClaimsPrincipalFactory where TAccount : RemoteUserAccount
{
private readonly IAccessTokenProviderAccessor _accessor;
#pragma warning disable PUB0001 // Pubternal type in public API
- public UserFactory(IAccessTokenProviderAccessor accessor) => _accessor = accessor;
+ public AccountClaimsPrincipalFactory(IAccessTokenProviderAccessor accessor) => _accessor = accessor;
///
/// Gets or sets the .
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs
index 030160bfee..00fcc8ba2d 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs
@@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
///
/// Gets the to map accounts to .
///
- protected UserFactory UserFactory { get; }
+ protected AccountClaimsPrincipalFactory UserFactory { get; }
///
/// Gets the options for the underlying JavaScript library handling the authentication operations.
@@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
IJSRuntime jsRuntime,
IOptions> options,
NavigationManager navigation,
- UserFactory userFactory)
+ AccountClaimsPrincipalFactory userFactory)
{
JsRuntime = jsRuntime;
Navigation = navigation;
@@ -159,7 +159,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
result.RedirectUrl = redirectUrl.ToString();
}
- return new AccessTokenResult(parsedStatus, result.Token, result.RedirectUrl);
+ return new AccessTokenResult(parsedStatus, result.Token, Navigation, result.RedirectUrl);
}
///
@@ -184,7 +184,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
result.RedirectUrl = redirectUrl.ToString();
}
- return new AccessTokenResult(parsedStatus, result.Token, result.RedirectUrl);
+ return new AccessTokenResult(parsedStatus, result.Token, Navigation, result.RedirectUrl);
}
private Uri GetRedirectUrl(string customReturnUrl)
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs
index 0134071a65..b7c1af973c 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/WebAssemblyAuthenticationServiceCollectionExtensions.cs
@@ -47,7 +47,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddScoped();
services.TryAddScoped();
- services.TryAddScoped>();
+ services.TryAddScoped>();
return new RemoteAuthenticationBuilder(services);
}
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs
index 2493b43dd0..5b8334ef6e 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs
@@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.SignInResult = new InternalRemoteAuthenticationResult
@@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.SignInResult = new InternalRemoteAuthenticationResult
@@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.CompleteSignInResult = new InternalRemoteAuthenticationResult
@@ -117,7 +117,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.CompleteSignInResult = new InternalRemoteAuthenticationResult
@@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.SignOutResult = new InternalRemoteAuthenticationResult
@@ -175,7 +175,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.SignOutResult = new InternalRemoteAuthenticationResult
@@ -202,7 +202,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.CompleteSignOutResult = new InternalRemoteAuthenticationResult
@@ -233,7 +233,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.CompleteSignOutResult = new InternalRemoteAuthenticationResult
@@ -260,7 +260,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -298,7 +298,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -338,7 +338,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
var state = new RemoteAuthenticationState();
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -379,7 +379,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
testJsRuntime,
options,
new TestNavigationManager(),
- new UserFactory(Mock.Of()));
+ new AccountClaimsPrincipalFactory(Mock.Of()));
testJsRuntime.GetUserResult = default;
@@ -571,7 +571,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
}
}
- internal class TestUserFactory : UserFactory
+ internal class TestUserFactory : AccountClaimsPrincipalFactory
{
public TestUserFactory(IAccessTokenProviderAccessor accessor) : base(accessor)
{
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs
index cb3f6b025e..54649ad129 100644
--- a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs
+++ b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs
@@ -113,10 +113,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
await renderer.Dispatcher.InvokeAsync