Skip to content
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

.NET 8 Blazor form processing with action to Razor Components Page with static SSR #56445

Open
1 task done
likquietlyy opened this issue Jun 25, 2024 · 3 comments
Open
1 task done
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-ssr investigate

Comments

@likquietlyy
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have Login.razor page(@page "/login") with InteractiveServer Render Mode. There is a <form> on this page with method="post", @formname="uniqueFormName" and action="login/submit"(@page "login/submit" with Static SSR mode). On submitting this form I get an error on page login/submit.

Expected Behavior

Login.razor page with InteractiveServer Render Mode(cause I need some interactivity on this page), and LoginSubmit.razor page with static SSR to process a post request from form on login page.

Steps To Reproduce

Login.razor

@page "/login"

<form method="post" action="login/submit" @formname="uniqueLoginForm">
  <input id="name" class="form-control" name="name" type="text"
         placeholder="User Name" required autofocus>
  <input id="password" class="form-control" name="password" type="password"
         placeholder="Password" required>
  <label class="form-check-label" for="rememberMe">
    <input id="rememberMe" class="form-check-input" name="rememberMe" type="checkbox" value="true">
    <span>Remember Me</span>
  </label>
  <input type="submit" value="Login"/>
  <AntiforgeryToken></AntiforgeryToken>
</form>

LoginSubmit.razor

@page "/login/submit"
@inject IHttpContextAccessor HttpContextAccessor
@inject NavigationManager NavigationManager

@code {
  protected override async Task OnParametersSetAsync()
  {
    ArgumentNullException.ThrowIfNull(HttpContextAccessor.HttpContext);

    var name = HttpContextAccessor.HttpContext.Request.Form["name"];
    var claims = new List<Claim> { new(ClaimTypes.Name, name) };

    var claimsIdentity = new ClaimsIdentity(claims, "UI");
    var userPrincipal = new ClaimsPrincipal([claimsIdentity]);

    await HttpContextAccessor.HttpContext.SignInAsync(userPrincipal, new AuthenticationProperties
    {
      IsPersistent = true
    });
  }
}

App.razor

  <HeadOutlet @rendermode="@RenderModeForPage" />
  <Routes @rendermode="@RenderModeForPage" />
@code {
  [CascadingParameter]
  private HttpContext HttpContext { get; set; } = default!;

  private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/login/submit")
      ? null
      : new InteractiveServerRenderMode();
}

Exceptions (if any)

The POST request does not specify which form is being submitted. To fix this, ensure <form> elements have a @formname attribute with any unique value, or pass a FormName parameter if using <EditForm>.

.NET Version

8.0.302

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jun 25, 2024
@MackinnonBuck
Copy link
Member

Thanks for reaching out.

When submitting a form across pages, the page being submitted to needs to specify a form with the specified @formname. So, you could put an empty form on the LoginSubmit page containing a hidden form with an @onsubmit handler. Does that work for you?

@MackinnonBuck
Copy link
Member

Long-term, we could address this in #53129

@MackinnonBuck MackinnonBuck added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jun 25, 2024
@likquietlyy
Copy link
Author

@MackinnonBuck, It works for me only for SSR form component that submit as HTTP request to SSR endpoint. But if I have form at Interactive Server render mode page that submit as HTTP request to SSR endpoint it still ends with an error message (The POST request does not specify which form is being submitted. To fix this, ensure <form> elements have a @formname attribute with any unique value, or pass a FormName parameter if using <EditForm>.)

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jun 26, 2024
@mkArtakMSFT mkArtakMSFT added investigate and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jun 26, 2024
@mkArtakMSFT mkArtakMSFT added this to the 9.0-rc1 milestone Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-ssr investigate
Projects
None yet
Development

No branches or pull requests

3 participants