+
Skip to content

[Question] How do we set a storage item on a button click? #251

@MrYossu

Description

@MrYossu

I'm using the .NET8 Blazor web app template, with the Identity stuff included.

I would like to set a local storage item when the user logs in, so added a single line to the Login.razor method that is called when the form is submitted...

  public async Task LoginUser() {
    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, set lockoutOnFailure: true
    SignInResult result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
    if (result.Succeeded) {
      await LocalStorage.SetItemAsStringAsync("Greeting", Input.Salutation);
      Logger.LogInformation("User logged in.");
      RedirectManager.RedirectTo(ReturnUrl);
    } else if (result.RequiresTwoFactor) {
      RedirectManager.RedirectTo(
        "Account/LoginWith2fa",
        new() { ["returnUrl"] = ReturnUrl, ["rememberMe"] = Input.RememberMe });
    } else if (result.IsLockedOut) {
      Logger.LogWarning("User account locked out.");
      RedirectManager.RedirectTo("Account/Lockout");
    } else {
      _errorMessage = "Error: Invalid login attempt.";
    }
  }

In this case, I have added a Salutation property to the InputModel, and want to store that in local storage.

However, when this is run, I get an exception....

InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method

OK, so I added the following at the top of the file...

@rendermode @(new InteractiveServerRenderMode(prerender: false))

However, that gives a different exception...

System.InvalidOperationException: Headers are read-only, response has already started

I'm a bit stuck now. Anyone able to explain how I do this?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionQuestion about this projectTriageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载