Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

generate jwt token authorize(roles = admin ) not working .net core 403

// Get User roles and add them to claims
                var roles = await _userManager.GetRolesAsync(user);
                AddRolesToClaims(claims, roles);

// ===== Token =====
        private async Task<object> GenerateJwtToken(User user)
        {
            var claims = new List<Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.NameIdentifier, user.Id),
            };

            // Get User roles and add them to claims
            var roles = await _userManager.GetRolesAsync(user);
            AddRolesToClaims(claims, roles);

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtKey"]));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expires = DateTime.Now.AddDays(Convert.ToDouble(_configuration["JwtExpireDays"]));

            var token = new JwtSecurityToken(
                _configuration["JwtIssuer"],
                _configuration["JwtIssuer"],
                claims,
                expires: expires,
                signingCredentials: creds
            );

            return new JwtSecurityTokenHandler().WriteToken(token);
        }


        private void AddRolesToClaims(List<Claim> claims, IEnumerable<string> roles)
        {
            foreach (var role in roles)
            {
                var roleClaim = new Claim(ClaimTypes.Role, role);
                claims.Add(roleClaim);
            }
        }
Comment

generate jwt token authorize(roles = admin ) not working .net core 403

var claims = new List<Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.NameIdentifier, user.Id),
                new Claim(ClaimTypes.Role,"The Role Of the logged in user, you can get from your DB")
            };
Comment

PREVIOUS NEXT
Code Example
Csharp :: copy array to array in c# 
Csharp :: Getting the ID of the element that fired an event 
Csharp :: visibility bound to radio button wpf 
Csharp :: linked list follow what in c# 
Csharp :: select startup item visual studio 2019 
Csharp :: Debug output to console and a log 
Csharp :: TextBox filling in C# 
Csharp :: händelsereportage 
Csharp :: c# move picturebox 
Csharp :: check null type 
Csharp :: how to make game restart when player touches a object unity 
Csharp :: unity check if swipe not tap 
Csharp :: hardcode dropdown cshtml 
Csharp :: c# variables 
Csharp :: quine in c# 
Csharp :: how to set the forgound color of listitems in c# 
Csharp :: Game of two stack c# 
Csharp :: unitydont play sound until finsihed 
Csharp :: reflection static method c# 
Csharp :: how to remove a parten transform unity 
Csharp :: how to make a 2d character move in unity 2020 
Csharp :: what loops are entry controlled c# 
Csharp :: c# exec command output 
Csharp :: unity AppDomain 
Csharp :: c# Detect Cycle in a Directed Graph 
Csharp :: c# return error status code based on exception 
Csharp :: get user by username c# 
Csharp :: save checkbox value to database c# 
Csharp :: C# replace all . except last one 
Csharp :: Convert any class to a keyvaluepair 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =