-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Blazor] Auth improvements #20073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Blazor] Auth improvements #20073
Changes from all commits
296a806
ade8d3e
bdf6979
5943149
fc4bc4b
eaecb5d
ea7bec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,23 @@ public static class MsalWebAssemblyServiceCollectionExtensions | |
/// <param name="configure">The <see cref="Action{RemoteAuthenticationOptions{MsalProviderOptions}}"/> to configure the <see cref="RemoteAuthenticationOptions{MsalProviderOptions}"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddMsalAuthentication(this IServiceCollection services, Action<RemoteAuthenticationOptions<MsalProviderOptions>> configure) | ||
{ | ||
return AddMsalAuthentication<RemoteAuthenticationState>(services, configure); | ||
} | ||
|
||
/// <summary> | ||
/// Adds authentication using msal.js to Blazor applications. | ||
/// </summary> | ||
/// <typeparam name="TRemoteAuthenticationState">The type of the remote authentication state.</typeparam> | ||
/// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
/// <param name="configure">The <see cref="Action{RemoteAuthenticationOptions{MsalProviderOptions}}"/> to configure the <see cref="RemoteAuthenticationOptions{MsalProviderOptions}"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddMsalAuthentication<TRemoteAuthenticationState>(this IServiceCollection services, Action<RemoteAuthenticationOptions<MsalProviderOptions>> configure) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the |
||
where TRemoteAuthenticationState : RemoteAuthenticationState, new() | ||
{ | ||
services.AddRemoteAuthentication<RemoteAuthenticationState, MsalProviderOptions>(); | ||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<RemoteAuthenticationOptions<MsalProviderOptions>>, MsalDefaultOptionsConfiguration>()); | ||
|
||
if (configure != null) | ||
{ | ||
services.Configure(configure); | ||
} | ||
|
||
return services; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,15 +253,26 @@ class OidcAuthorizeService implements AuthorizeService { | |
export class AuthenticationService { | ||
|
||
static _infrastructureKey = 'Microsoft.AspNetCore.Components.WebAssembly.Authentication'; | ||
static _initialized = false; | ||
static _initialized : Promise<void>; | ||
static instance: OidcAuthorizeService; | ||
|
||
public static async init(settings: UserManagerSettings & AuthorizeServiceSettings) { | ||
// Multiple initializations can start concurrently and we want to avoid that. | ||
// In order to do so, we create an initialization promise and the first call to init | ||
// tries to initialize the app and sets up a promise other calls can await on. | ||
if (!AuthenticationService._initialized) { | ||
AuthenticationService._initialized = true; | ||
const userManager = await this.createUserManager(settings); | ||
AuthenticationService.instance = new OidcAuthorizeService(userManager); | ||
this._initialized = new Promise(async (resolve, reject) => { | ||
try { | ||
const userManager = await this.createUserManager(settings); | ||
AuthenticationService.instance = new OidcAuthorizeService(userManager); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor stylistic point but rather than manually creating a promise and having a try/catch, it could do: this._initialized = (async () => {
const userManager = await this.createUserManager(settings);
AuthenticationService.instance = new OidcAuthorizeService(userManager);
})(); |
||
} | ||
|
||
await this._initialized; | ||
} | ||
|
||
public static getUser() { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,14 @@ | |||||
<Found Context="routeData"> | ||||||
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> | ||||||
<NotAuthorized> | ||||||
<RedirectToLogin /> | ||||||
@if(!context.User.Identity.IsAuthenticated) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
<RedirectToLogin /> | ||||||
} | ||||||
else | ||||||
{ | ||||||
<p>You are not authorized to access this resource.</p> | ||||||
} | ||||||
</NotAuthorized> | ||||||
</AuthorizeRouteView> | ||||||
</Found> | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.