Click here to Skip to main content
15,887,027 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Dumb it down for one who's basically a beginner Pin
Andre Oosthuizen16-Nov-23 5:36
mveAndre Oosthuizen16-Nov-23 5:36 
Questioncan't build blazor library in azure pipeline Pin
Super Lloyd6-Sep-23 17:08
Super Lloyd6-Sep-23 17:08 
AnswerRe: can't build blazor library in azure pipeline Pin
Super Lloyd6-Sep-23 18:27
Super Lloyd6-Sep-23 18:27 
QuestionBlazor component debugging Pin
Super Lloyd23-Aug-23 14:16
Super Lloyd23-Aug-23 14:16 
AnswerRe: Blazor component debugging Pin
Super Lloyd24-Aug-23 17:27
Super Lloyd24-Aug-23 17:27 
QuestionHTML SELECT CONTROL WITH RUNAT="SERVER Pin
tchia_k30-Jul-23 3:53
professionaltchia_k30-Jul-23 3:53 
AnswerRe: HTML SELECT CONTROL WITH RUNAT="SERVER Pin
Richard Deeming30-Jul-23 22:14
mveRichard Deeming30-Jul-23 22:14 
Question"Context.UserIdentifier" of SignalR is always null when I use CustomAuthenticationStateProvider in Blazor Server App Pin
Alex Wright 202221-Jul-23 3:45
Alex Wright 202221-Jul-23 3:45 
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<AuthenticationState> GetAuthenticationStateAsync()
        {
            try
            {
                var userSessionStorageResult = await _sessionStorage.GetAsync<UserSession>("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<Claim> {
                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<Claim>
                {
                    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;
            }

            NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal)));
        }
    }


UserSession.cs

public class UserSession
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public string UserRole { get; set; }
    public string Name { get; set; }
    public string TokenText { get; set; }
}


LoginController:

[Route("api/[controller]/[action]")]
    [ApiController]
    public class ApiLoginController : ControllerBase
    {
        private readonly SqliteContext _sqlServerContext;
        private readonly IConfiguration _configuration;
        private readonly IUserService _userService;

        public ApiLoginController(SqliteContext sqlServerContext, IConfiguration configuration, IUserService userService)
        {
            _sqlServerContext = sqlServerContext;
            _configuration = configuration;
            _userService = userService;
        }

        [HttpPost]
        public async Task<IActionResult> LoginSystem([FromBody] UserLoginVM loginModel)
        {
            var user = await _sqlServerContext.Users.Include(x => x.RoleRefNavigation)
                .FirstOrDefaultAsync(x => x.Username == loginModel.Username && x.IsActive);
            if (user == null)
            {
                return BadRequest("Invalid credentials.");
            }

            if (!MatchPasswordHash(loginModel.Password, user.Password, user.SaltPassword))
            {
                return BadRequest("Invalid credentials.");
            }
            if (!user.IsActive)
            {
                return StatusCode(403, "User is not active.");
            }
            if (user.IsLocked)
            {
                DateTime setDate = (DateTime)user.LockUntil;
                DateTime current = DateTime.Now;
                if (setDate > current)
                {
                    return StatusCode(403, "User is restricted.");
                }
                await _userService.UnsetUserLimits(user.UserId);
            }

            user.RoleRefNavigation = await _sqlServerContext.Roles.FirstOrDefaultAsync(x => x.RoleId == user.RoleRef);
            string token = CreateToken(user);

            var data = new
            {
                tokenText = token,
                username = user.Username,
                userId = user.UserId.ToString(),
                name = user.Name,
                role = user.RoleRefNavigation.User_Role
            };

            await _userService.RegisterLoginTime(user.UserId);

            return Ok(data);
        }

        private string CreateToken(User user)
        {
            List<Claim> claims = new List<Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, user.Username),
                new Claim(ClaimTypes.Role, user.RoleRefNavigation.User_Role),
                new Claim(type: "UserId", value: user.UserId.ToString())
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration.GetSection("Jwt:Key").Value!));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
            var token = new JwtSecurityToken(
                claims: claims,
                issuer: _configuration["Jwt:Issuer"],
                audience: _configuration["Jwt:Issuer"],
                expires: DateTime.Now.AddHours(8),
                signingCredentials: creds
                );

            var jwt = new JwtSecurityTokenHandler().WriteToken(token);

            return jwt;
        }

        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;
            }
        }
    }


The problem is that when I check Context.User?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; in SignalR hub, Context.UserIdentifier is always null. How can I fix this?

modified 21-Jul-23 9:51am.

SuggestionRe: "Context.UserIdentifier" of SignalR is always null when I use CustomAuthenticationStateProvider in Blazor Server App Pin
Richard Deeming23-Jul-23 23:17
mveRichard Deeming23-Jul-23 23:17 
QuestionIs is possible to import an excel file with hyperlinks? Pin
samflex16-Jul-23 17:25
samflex16-Jul-23 17:25 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
Richard MacCutchan16-Jul-23 21:45
mveRichard MacCutchan16-Jul-23 21:45 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
samflex17-Jul-23 5:26
samflex17-Jul-23 5:26 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
Richard MacCutchan17-Jul-23 5:29
mveRichard MacCutchan17-Jul-23 5:29 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
samflex18-Jul-23 3:43
samflex18-Jul-23 3:43 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
Richard MacCutchan18-Jul-23 4:40
mveRichard MacCutchan18-Jul-23 4:40 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
RedDk18-Jul-23 7:19
RedDk18-Jul-23 7:19 
AnswerRe: Is is possible to import an excel file with hyperlinks? Pin
Andre Oosthuizen16-Jul-23 23:59
mveAndre Oosthuizen16-Jul-23 23:59 
AnswerRe: Is is possible to import an excel file with hyperlinks? Pin
Richard Andrew x6424-Jul-23 3:19
professionalRichard Andrew x6424-Jul-23 3:19 
GeneralRe: Is is possible to import an excel file with hyperlinks? Pin
samflex24-Jul-23 6:34
samflex24-Jul-23 6:34 
QuestionIllustrating ad emails Pin
Ali Al Omairi(Abu AlHassan)10-Jul-23 2:35
professionalAli Al Omairi(Abu AlHassan)10-Jul-23 2:35 
QuestionAdvise on how to reuse code behind in asp.net VB pages Pin
Member 875830229-Jun-23 16:25
Member 875830229-Jun-23 16:25 
AnswerRe: Advise on how to reuse code behind in asp.net VB pages Pin
Andre Oosthuizen3-Jul-23 0:49
mveAndre Oosthuizen3-Jul-23 0:49 
GeneralRe: Advise on how to reuse code behind in asp.net VB pages Pin
Member 87583024-Jul-23 3:04
Member 87583024-Jul-23 3:04 
GeneralRe: Advise on how to reuse code behind in asp.net VB pages Pin
Andre Oosthuizen4-Jul-23 3:40
mveAndre Oosthuizen4-Jul-23 3:40 
AnswerRe: Advise on how to reuse code behind in asp.net VB pages Pin
Gerry Schmitz3-Jul-23 4:54
mveGerry Schmitz3-Jul-23 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.