Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

User authorization in Blazor

$
0
0

I have a problem with the method that authorizes users

  private async Task LogUsername()  {      var authState = await authenticationStateTask;      var user = authState.User;      if (user.Identity.IsAuthenticated)      {          LoggedUserCode = user.Identity.Name;      }  }

Method call:

    protected override async Task OnInitializedAsync()    {        await LogUsername();        if (!String.IsNullOrEmpty(LoggedUserCode))            user1.Id = Authentication.UserAccountService._users.FirstOrDefault(x => x.Kod == LoggedUserCode).Id;        element = await Task.Run(() => adminPanels.GetUser(user1.Id));        mg = await Task.Run(() => wS.GetWarehouse(element.magazyn));        // okuciaObj = await Task.Run(() => VikkingOkucia.PobierzOkucia(mg.id));        _table?.SetEditingItem(null);    }

but after refreshing the browser I have only nulls in the values ​​and dapper crashes.CustomAuthentitactionStateProvider.cs

using Microsoft.AspNetCore.Components.Authorization;using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;using System.Security.Claims;namespace VikkingWebApp.Authentication{    public class CustomAuthentitactionStateProvider : AuthenticationStateProvider    {        private readonly ProtectedSessionStorage _sessionStorage;        private ClaimsPrincipal _anonymous = new ClaimsPrincipal(new ClaimsIdentity());        public CustomAuthentitactionStateProvider(ProtectedSessionStorage sessionStorage)        {            _sessionStorage = sessionStorage;        }        public override async Task<AuthenticationState> GetAuthenticationStateAsync()        {            try            {                var userSessionStorageResoult = await _sessionStorage.GetAsync<UserSession>("UserSession");                var userSession = userSessionStorageResoult.Success ? userSessionStorageResoult.Value : null;                if (userSession == null)                    return await Task.FromResult(new AuthenticationState(_anonymous));                var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>                {                    new Claim(ClaimTypes.Name, userSession.UserCode),                    new Claim(ClaimTypes.Role, userSession.Role)                }, "CustomAuth"));                return await Task.FromResult(new AuthenticationState(claimsPrincipal));            }            catch            {                return await Task.FromResult(new AuthenticationState(_anonymous));            }        }        public async Task UpdateAuthenticactionState(UserSession userSession)        {            ClaimsPrincipal claimsPrincipal;            if (userSession != null)            {                await _sessionStorage.SetAsync("UserSession", userSession);                claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>                    {                        new Claim(ClaimTypes.Name, userSession.UserCode),                        new Claim(ClaimTypes.Role, userSession.Role)                    }));            }            else            {                await _sessionStorage.DeleteAsync("UserSession");                claimsPrincipal = _anonymous;            }            NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal)));        }    }}

Program.cs

builder.Services.AddScoped<ProtectedSessionStorage>();builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthentitactionStateProvider>();

I see that it is only called once public override async Task<AuthenticationState> GetAuthenticationStateAsync()and after refreshing the browser it is not called at all.


Viewing all articles
Browse latest Browse all 4839

Trending Articles