title: .Net Core 3 Blazor Server - How to do basic login with claims?
body: <p>I already have tables with usernames, passwords, and role IDs, so I do not want to use the ASP.NET Membership tables.  And I only need a simple login page that asks for username and password, not their pre-built Identity.UI package.</p>
  
  <p>But I do not know how to get Blazor Server to do a basic login for me.  I have what I believe should be the basics configured in Startup.cs, such as:</p>
  
  <p>In ConfigureServices:</p>
  
  <pre><code>services
      .AddAuthentication(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme)
      .AddCookie();
  </code></pre>
  
  <p>In Configure():</p>
  
  <pre><code>app.UseAuthentication();
  app.UseAuthorization();
  </code></pre>
  
  <p>Normally I would call something like this:</p>
  
  <pre><code>await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, 
      principal, 
      new AuthenticationProperties { IsPersistent = loginData.RememberMe });
  </code></pre>
  
  <p>And in fact the above <strong>does</strong> work if I run it from a .cshtml page, but I want this to work as a Blazor component (.razor).</p>
  
  <p>I tried injecting <code>httpContextAccessor</code> but trying to call the <code>SignInAsync</code> method from there resulted in an error about the headers already being set.</p>
  
  <p>I have a .razor Login component which I don't know what to call to do an actual sign in.</p>
  
  <pre><code>@page "/login"
  
  
  @using Microsoft.AspNetCore.Authentication.Cookies;
  @using Microsoft.AspNetCore.Components;
  @using System.Security.Claims;
  
  
  @inject NavigationManager navigationManager
  @inject DataAccess.Models.CurrentDataContext db
  
  &lt;h3&gt;Login&lt;/h3&gt;
  
  &lt;div&gt;
      Email:
      &lt;input type="text" @bind="@email" /&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      Password:
      &lt;input type="text" @bind="@password" /&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;button class="btn btn-primary" @onclick="DoLogin"&gt;Log In&lt;/button&gt;
      &lt;span class="text-danger"&gt;@loginErrorMessage&lt;/span&gt;
  &lt;/div&gt;
  
  @code {
      string email = "";
      string password = "";
      string loginErrorMessage = "";
  
      protected async Task DoLogin()
      {
          var emailTrimmed = email.ToLower().Trim();
          var user = db.UserAccount.FirstOrDefault(u =&gt; u.EmailAddress.ToLower() == emailTrimmed);
          if (user != null) //if (user != null)
          {
  
              //TODO check password
  
  
              var claims = new List&lt;Claim&gt; { 
                  new Claim(ClaimTypes.Name, emailTrimmed)
              };
  
              var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  
  
              //SIGN in the user
  
              ///////////////////////////
              //   WHAT GOES HERE?   ////
              ///////////////////////////
              //
  
              navigationManager.NavigateTo("/fetchdata");
  
          }
          else
          {
              loginErrorMessage = "Error logging in.";
          }
      }
  }
  </code></pre>