neozhu / CleanArchitectureWithBlazorServer

This is a repository for creating a Blazor Server dashboard application following the principles of Clean Architecture
https://architecture.blazorserver.com/
MIT License
763 stars 213 forks source link

Error on sign in when signing in from page that is using `PublicLayout` #701

Closed rwxzig closed 1 month ago

rwxzig commented 1 month ago

I am getting an error when I click sign in and the home page is a page that is using the public layout. My case is that I have created a page "Home" that looks something like this:

@page "/"
@layout PublicLayout
*
*
*
@code {
*
*
*
}

In this case it returns this error (below) when I click sign in:

Unhandled exception in circuit 'F6NwJh2XUPSkGnzf0-IzSmthsvNKs_u4ID3Nmq1EyDQ'.
System.InvalidOperationException: Headers are read-only, response has already started.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_SetCookie(StringValues value)
   at Microsoft.AspNetCore.Http.ResponseCookies.Append(ReadOnlySpan`1 keyValuePairs, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.AppendResponseCookie(HttpContext context, String key, String value, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext context, String scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable`1 additionalClaims)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInOrTwoFactorAsync(TUser user, Boolean isPersistent, String loginProvider, Boolean bypassTwoFactor)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(TUser user, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at CleanArchitecture.Blazor.Server.UI.Pages.Identity.Authentication.Login.OnSubmit() in C:\projects\CleanArchitecture\src\Server.UI\Pages\Identity\Authentication\Login.razor:line 116
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

When Dashboard page is set as homepage, everything is fine. Any idea on how to fix this behaviour?

neozhu commented 1 month ago

refer to public/index.razor in the project, this page is created from PublicLayout. and if not login can be navigated to Login.

rwxzig commented 1 month ago

I tried and I get the same behaviour, when I click sign in, the error is raised, and unless the user performs a reload, it wont be able to sign in.

Edit.1:

Changed the title because this error occurs when signing in from a page that is using PublicLayout

neozhu commented 1 month ago

I tried and I get the same behaviour, when I click sign in, the error is raised, and unless the user performs a reload, it wont be able to sign in.

Edit.1:

Changed the title because this error occurs when signing in from a page that is using PublicLayout

<MudButton Variant="Variant.Text" Href="/pages/authentication/login" StartIcon="@Icons.Material.Rounded.Login" Style="text-transform:none"> @L["Login"] OR RedirectManager.RedirectTo(LoginPage url);

rwxzig commented 1 month ago

Could you elaborate a bit more? Thanks in advance.

neozhu commented 1 month ago

Could you elaborate a bit more?

Thanks in advance.

Could you share your code

rwxzig commented 1 month ago

The only difference that I have is that Dashboard.razor is no longer home route, instead it is:

@page "/dashboard"
@using CleanArchitecture.Blazor.Server.UI.Pages.Dashboard.Components
@using MudBlazor.Utilities
@inject IStringLocalizer<Dashboard> L
<PageTitle>@Title</PageTitle>

And public /Public/Index.razor is set as home route page:

@page "/"
<PageTitle>@Title</PageTitle>
<h2>Clean Architecture With Blazor Server</h2>
<p>This repository guides ... </p>
@code {
}

Edit.1

Just to add to the context, I have a use case where I must use the PublicLayout as layout in my home page and being able to allow the user to sign in from that page.

neozhu commented 1 month ago

image

No problems with my debugging, change public\index.razor to @page "/" change Dashboard\dashboard.razor @page "/dashboard/index"

rwxzig commented 1 month ago

I created a small video to demonstrate the issue. Here is a private youtube link so that you can see (39 sec). I tried to capture all the essential information, in case that you want to replicate. It a clean git clone, and only two files are changed, as demonstrated on the clip.