Skip to content

Fix Blazor template bug where a logged in user could appear to be unauthenticated #51497

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

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,29 @@ namespace BlazorWeb_CSharp.Client;
// This only provides a user name and email for display purposes. It does not actually include any tokens
// that authenticate to the server when making subsequent requests. That works separately using a
// cookie that will be included on HttpClient requests to the server.
public class PersistentAuthenticationStateProvider(PersistentComponentState persistentState) : AuthenticationStateProvider
internal class PersistentAuthenticationStateProvider : AuthenticationStateProvider
{
private static readonly Task<AuthenticationState> unauthenticatedTask =
private static readonly Task<AuthenticationState> defaultUnauthenticatedTask =
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));

public override Task<AuthenticationState> GetAuthenticationStateAsync()
private readonly Task<AuthenticationState> authenticationStateTask = defaultUnauthenticatedTask;

public PersistentAuthenticationStateProvider(PersistentComponentState state)
{
if (!persistentState.TryTakeFromJson<UserInfo>(nameof(UserInfo), out var userInfo) || userInfo is null)
if (!state.TryTakeFromJson<UserInfo>(nameof(UserInfo), out var userInfo) || userInfo is null)
{
return unauthenticatedTask;
return;
}

Claim[] claims = [
new Claim(ClaimTypes.NameIdentifier, userInfo.UserId),
new Claim(ClaimTypes.Name, userInfo.Email),
new Claim(ClaimTypes.Email, userInfo.Email) ];

return Task.FromResult(
authenticationStateTask = Task.FromResult(
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claims,
authenticationType: nameof(PersistentAuthenticationStateProvider)))));
}

public override Task<AuthenticationState> GetAuthenticationStateAsync() => authenticationStateTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data

@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc
@using BlazorWeb_CSharp.Data

@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.ViewFeatures
@using BlazorWeb_CSharp.Data

@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
@page "/Account/Manage"

@using System.ComponentModel.DataAnnotations
@using System.Security.Claims
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data

@inject AuthenticationStateProvider AuthenticationStateProvider
@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
@inject IdentityUserAccessor UserAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@using System.ComponentModel.DataAnnotations
@using System.Text
@using System.Text.Encodings.Web
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ else
private HttpContext HttpContext { get; set; } = default!;

[SupplyParameterFromQuery]
public string? Email { get; set; }
private string? Email { get; set; }

[SupplyParameterFromQuery]
public string? ReturnUrl { get; set; }
private string? ReturnUrl { get; set; }

protected override async Task OnInitializedAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@using System.Text
@using System.Text.Encodings.Web
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

@using System.ComponentModel.DataAnnotations
@using System.Text
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else

@code {
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
private HttpContext? HttpContext { get; set; }

protected override void OnParametersSet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: InteractiveAuto;
}
Expand All @@ -59,7 +59,7 @@
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: InteractiveServer;
}
Expand All @@ -69,7 +69,7 @@
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: InteractiveWebAssembly;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@*#if (IndividualLocalAuth)
@implements IDisposable

@inject NavigationManager NavigationManager

##endif*@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

@code{
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
private HttpContext? HttpContext { get; set; }

public string? RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
private string? RequestId { get; set; }
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

protected override void OnInitialized() =>
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
Expand Down