0

I am using Microsoft Visual Studio Community 2022 (64-bit) v17.14.16. I have followed your code but I am getting error in OnValidSubmitAsync when the function

 private async Task OnValidSubmitAsync()
 {
     var firstName = user.FirstName;
     var lastName = user.LastName;

     if (Input.FirstName != firstName)
     {
         user.FirstName = Input.FirstName;
         await UserManager.UpdateAsync(user);
     }

     if (Input.LastName != lastName)
     {
         user.LastName = Input.LastName;
         await UserManager.UpdateAsync(user);
     }

     if (Input.ProfilePicture != null && Input.ProfilePicture.Length > 0)
     {
         user.ProfilePicture = Input.ProfilePicture;
         await UserManager.UpdateAsync(user);
     }

     if (Input.PhoneNumber != phoneNumber)
     {
         var setPhoneResult = await UserManager.SetPhoneNumberAsync(user, Input.PhoneNumber);

         if (!setPhoneResult.Succeeded)
         {
             RedirectManager.RedirectToCurrentPageWithStatus("Error: Failed to set phone number.", HttpContext);
         }
     }

     await SignInManager.RefreshSignInAsync(user);

     RedirectManager.RedirectToCurrentPageWithStatus("Your profile has been updated", HttpContext);
}

This error in the console:

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'yahbi1W7qDkUrcjvWNvKuK3B755old6G346MHD5HLfQ'.
      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(String key, String value, 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.RefreshSignInAsync(TUser user)
         at BlazorApp1.Components.Account.Pages.Manage.Index.OnValidSubmitAsync() in C:\Users\aamir\source\repos\BlazorApp1\BlazorApp1\Components\Account\Pages\Manage\Index.razor:line 149
         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)
dbug: Microsoft.AspNetCore.SignalR.HubConnectionHandler[6]
      OnConnectedAsync ending.

I tried to save first name, last name and profile picture in the class ApplicationUser derived from IdentityUser. The data is saving after following the code but getting error message on a function. On page reload from browser the error message disappears and the data is loaded correctly.

3
  • Hi Aamir, Could you try removing ` await SignInManager.RefreshSignInAsync(user);` and doing a force page refresh instead? Commented Sep 25 at 15:08
  • It again gives error on RedirectManager.RedirectToCurrentPageWithStatus("Your profile has been updated", HttpContext); And the user is not refreshed resulting in log out. Commented Sep 26 at 7:49
  • I am using code from this post: stackoverflow.com/questions/77354692/… Commented Sep 26 at 8:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.