title: How to authenticate a user with Blazor Server
body: <p>I have a Blazor Server application that uses MongoDB as the database so I'm trying to implement authentication with that. So I can use the <code>&lt;Authenticted&gt;, &lt;AuthorizeView Roles="admin"&gt;</code> and other tags like that in the razor pages.</p>
  
  <p>The built-in authentication template uses SQL Server, which I don't want in this case, and there isn't a clear example of how to do it yourself with another database. Given the example Microsoft provides <a href="https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="noreferrer">here</a></p>
  
  <pre><code>using System.Security.Claims;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Components.Authorization;
  
  namespace BlazorSample.Services
  {
      public class CustomAuthStateProvider : AuthenticationStateProvider
      {
          public override Task&lt;AuthenticationState&gt; GetAuthenticationStateAsync()
          {
              var identity = new ClaimsIdentity(new[]
              {
                  new Claim(ClaimTypes.Name, "mrfibuli"),
              }, "Fake authentication type");
  
              var user = new ClaimsPrincipal(identity);
  
              return Task.FromResult(new AuthenticationState(user));
          }
      }
  }
  </code></pre>
  
  <p>How should one use this in an application? You obviously wouldn't hard code a single value, and value-type, as your only source of authenticating. So how should that be parameterized? With local attributes like:</p>
  
  <pre><code>Username { get; set; }
  UserType { get; set; }
  </code></pre>
  
  <p>In which case where would you set that?  </p>
  
  <p>Also how would you then use this to authenticate a user? I have the class added in the startup file under the <code>ConfigurationServices(...)</code> method:</p>
  
  <pre><code>...
  services.AddScoped&lt;AuthenticationStateProvider, MongoAuthenticationStateProvider&gt;();
  ...
  </code></pre>
  
  <p>I can't figure out how to authenticate anyone. I would imagine you validate the username and password in any number of ways, then when you know it's good you go ahead and update the authentication in .NET. I was following a tutorial where they suggested something like this in the code behind:</p>
  
  <pre><code>using System;
  using System.Linq;
  using DocsPlatform.Services;
  using System.Threading.Tasks;
  using System.Security.Claims;
  using Microsoft.AspNetCore.Http;
  using System.Collections.Generic;
  using Microsoft.AspNetCore.Components;
  using Microsoft.AspNetCore.Authentication;
  using Microsoft.AspNetCore.Authentication.Cookies;
  using Microsoft.AspNetCore.Components.Authorization;
  
  namespace DocsPlatform.Pages
  {
      public class LoginBase : ComponentBase
      {
          [CascadingParameter]
          private Task&lt;AuthenticationState&gt; authStateTask { get; set; }
          protected string username { get; set; }
          protected string password { get; set; }
  
          protected async Task LoginUser()
          {
              bool isValid = true;
  
              isValid = dbService.ValidateUser(username, password);
  
              string email = dbService.GetEmail(username);
  
              var claims = new List&lt;Claim&gt;
              {
                  new Claim(ClaimTypes.Name, username),
                  new Claim(ClaimTypes.Email, email),
              };
  
              var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
              await HttpContext.SignInAsync(
                  CookieAuthenticationDefaults.AuthenticationScheme,
                  new ClaimsPrincipal(claimsIdentity));
  
              return NavigationManager.NavigateTo("/");
          }
      }
  }
  
  </code></pre>
  
  <p>However, the navigation in the return doesn't work (they didn't explain how their code even compiled) and the SignInAsync() method isn't available in they way they showed. Again, I have no idea how their code compiled. So how would one normally do this? </p>
  
  <p>I can't find any tutorials, examples, etc other than the hundreds of <em>examples</em> that just use the built-in SQL Server template. Where can one find details on how this is used? Anything other than the "use the built-in template" or a link to the documention <a href="https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="noreferrer">here</a> would be appreciated as neither explain how to do this.</p>