Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. "Context.UserIdentifier" of SignalR is always null when I use CustomAuthenticationStateProvider in Blazor Server App

"Context.UserIdentifier" of SignalR is always null when I use CustomAuthenticationStateProvider in Blazor Server App

Scheduled Pinned Locked Moved ASP.NET
asp-netsysadminquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Wright 2022
    wrote on last edited by
    #1

    I'm working on Blazor server App project. I have the following codes for CustomAuthenticationStateProvider: CustomAuthenticationStateProvider.cs

    public class CustomAuthenticationStateProvider : AuthenticationStateProvider
    {
    private readonly ProtectedSessionStorage _sessionStorage;
    private ClaimsPrincipal _anonymous = new ClaimsPrincipal(new ClaimsIdentity());
    public CustomAuthenticationStateProvider(ProtectedSessionStorage sessionStorage)
    {
    _sessionStorage = sessionStorage;
    }

        public override async Task GetAuthenticationStateAsync()
        {
            try
            {
                var userSessionStorageResult = await \_sessionStorage.GetAsync("UserSession");
                var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null;
                if (userSession == null)
                {
                    return await Task.FromResult(new AuthenticationState(\_anonymous));
                }
                var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List {
                new Claim(ClaimTypes.Name, userSession.Username),
                new Claim(ClaimTypes.Role, userSession.UserRole),
                new Claim(ClaimTypes.NameIdentifier, userSession.UserId.ToString())
            }, "Jwt"));
    
                return await Task.FromResult(new AuthenticationState(claimsPrincipal));
            }
            catch (Exception)
            {
                return await Task.FromResult(new AuthenticationState(\_anonymous));
            }
        }
    
        public async Task UpdateAuthenticationState(UserSession userSession)
        {
            ClaimsPrincipal claimsPrincipal;
    
            if (userSession != null)
            {
                await \_sessionStorage.SetAsync("UserSession", userSession);
                await \_sessionStorage.SetAsync("Token", userSession.TokenText);
                claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List
                {
                    new Claim(ClaimTypes.Name, userSession.Username),
                    new Claim(ClaimTypes.Role, userSession.UserRole),
                    new Claim(ClaimTypes.NameIdentifier, userSession.UserId.ToString())
                }));
            }
            else
            {
                await \_sessionStorage.DeleteAsync("UserSession");
                claimsPrincipal = \_anonymous;
    
    Richard DeemingR 1 Reply Last reply
    0
    • A Alex Wright 2022

      I'm working on Blazor server App project. I have the following codes for CustomAuthenticationStateProvider: CustomAuthenticationStateProvider.cs

      public class CustomAuthenticationStateProvider : AuthenticationStateProvider
      {
      private readonly ProtectedSessionStorage _sessionStorage;
      private ClaimsPrincipal _anonymous = new ClaimsPrincipal(new ClaimsIdentity());
      public CustomAuthenticationStateProvider(ProtectedSessionStorage sessionStorage)
      {
      _sessionStorage = sessionStorage;
      }

          public override async Task GetAuthenticationStateAsync()
          {
              try
              {
                  var userSessionStorageResult = await \_sessionStorage.GetAsync("UserSession");
                  var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null;
                  if (userSession == null)
                  {
                      return await Task.FromResult(new AuthenticationState(\_anonymous));
                  }
                  var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List {
                  new Claim(ClaimTypes.Name, userSession.Username),
                  new Claim(ClaimTypes.Role, userSession.UserRole),
                  new Claim(ClaimTypes.NameIdentifier, userSession.UserId.ToString())
              }, "Jwt"));
      
                  return await Task.FromResult(new AuthenticationState(claimsPrincipal));
              }
              catch (Exception)
              {
                  return await Task.FromResult(new AuthenticationState(\_anonymous));
              }
          }
      
          public async Task UpdateAuthenticationState(UserSession userSession)
          {
              ClaimsPrincipal claimsPrincipal;
      
              if (userSession != null)
              {
                  await \_sessionStorage.SetAsync("UserSession", userSession);
                  await \_sessionStorage.SetAsync("Token", userSession.TokenText);
                  claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List
                  {
                      new Claim(ClaimTypes.Name, userSession.Username),
                      new Claim(ClaimTypes.Role, userSession.UserRole),
                      new Claim(ClaimTypes.NameIdentifier, userSession.UserId.ToString())
                  }));
              }
              else
              {
                  await \_sessionStorage.DeleteAsync("UserSession");
                  claimsPrincipal = \_anonymous;
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Quote:

      private bool MatchPasswordHash(string passwordText, byte[] password, byte[] passwordKey)
      {
      using (var hmac = new HMACSHA512(passwordKey))
      {
      var passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(passwordText));
      for (int i = 0; i < passwordHash.Length; i++)
      {
      if (passwordHash[i] != password[i])
      {
      return false;
      }
      }
      return true;
      }
      }

      Not an answer to your question, but that code is potentially vulnerable to a timing attack[^]. Although the salt may render it harder for an attacker to exploit, it would be better to avoid the early return - you always want this function to compare the full length of the arrays, not just the first n bytes.

      bool areEqual = true;
      for (int i = 0; i < passwordHash.Length; i++)
      {
      if (passwordHash[i] != password[i])
      {
      areEqual = false;
      }
      }

      return areEqual;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups