blazor SignInAsync have This Error 'Headers are read-only, response has already started.'

Viewed 1230

i make webpage by blazor i have problem to login and i use this normal code for login

    protected async Task LoginRegisterUser()
    {
        try
        {
            //if (Context.Response.HasStarted== false)
            //{
            //    await _next.Invoke(context);
            //}

                bool returnData = await CheckActiveCode(RegisterViewItem.PhoneNumber, RegisterViewItem.ActiveCode);
                if (returnData)//کد درست بود
                {
                    var usersItem = await userManagerService.SelectUserByPhoneNumberAsync(RegisterViewItem.PhoneNumber);

                    if (usersItem == null || usersItem.Id <= 0)
                    {
                        blazoredToast.ShowError("خطا در ورود کاربر");
                        return;
                    }
                   
                    var validatr = await signInManagerService.CheckPasswordSignInAsync(usersItem, usersItem.PhoneNumber, false);

                    if (validatr.Succeeded)
                    {
                         await signInManagerService.SignInAsync(usersItem, RegisterViewItem.RememberMe);
                        blazoredToast.ShowSuccess("کاربر در حال ورود", "خوش امدید");

                    }
                    else
                    {
                        blazoredToast.ShowError("رمز ورود کاربر");
                        return;
                    }
                }
                else//کد اشتباه است
                {
                    blazoredToast.ShowError("کد تایید اشتباه است");
                }

            
        }
        catch (Exception ex)
        {
            blazoredToast.ShowError(ex.Message);
        }
    }

i know this code is true but blazor can not manage Request HttpContext.

enter image description here

how can i use Invoke in this function ?

i use PasswordSignInAsync but not fix this problem and show again this error 'Headers are read-only, response has already started.'

4 Answers

If you are using blazor server you need to create a razor page to login and logout because in blazor you must not access HttpContext (Microsoft recommandation)

You have a sample here for logout but you can make the same for login.

You need just create a razor page and redirect from blazor to this page with _navigationManager.NavigateTo("you razor page", true);

in the razor page add the code :

public async Task OnGetAsync()
{
    await _SignInManager.SignInAsync(new ApplicationUser(){ UserName = "test@test.com" }, true);
    Response.Redirect("/");
}

You can also create a new razor project with authentication to show how it make it.

You need to understand the pipeline. The server takes in a request for a page or other asset.

It then starts to form the outgoing page, including User information (user name and so on). It sends this page (the response) out to the client.

AFTER that, it arrives at the Blazor rendering logic. So either the client is or isn't logged in already, and there's nothing that you can do programmatically. (well, there is, but it's not easier than just navigating to a login page)

When you try to log in from Blazor without making a new page request, further activities are NOT going through that pipeline, so there's no chance for the server to send the login info to the client.

Long story short-- you need to get the server to send a fresh page, and it can't (for the reasons I just mentioned) be Blazor.

In Blazor Server App, Context can not changes and it's read-only. as a result you must be develop middleware or implement your onboarding pages with razor and mvc controller. for developing middleware please follow this link : https://github.com/dotnet/aspnetcore/issues/13601

Related