Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot apply indexing with [] to an expression of type 'IConfiguration'

i need to create a login with jwt . when i use this code :

        private string BuildToken(User user)
    {

        var claims = new[] {
            new Claim(JwtRegisteredClaimNames.Sub, user.DisplayName),
            new Claim(JwtRegisteredClaimNames.Email, user.Email),
            new Claim(JwtRegisteredClaimNames.Birthdate, user.BirthDate.ToString()),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };

        var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
        var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

        var token = new JwtSecurityToken(_config["Jwt:Issuer"],
          _config["Jwt:Issuer"],
          claims,
          expires: DateTime.Now.AddMinutes(30),
          signingCredentials: creds);

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

it show me this error :

Severity Code Description Project File Line Suppression State Error CS0021 Cannot apply indexing with [] to an expression of type 'IConfiguration' StoreFinal C:\Users\Mr-Programer\Desktop\New folder\StoreFinal\StoreFinal\Areas\Admin\Controllers\LoginController.cs 65 Active

show me in error in this line : var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));

like image 720
Kianoush Avatar asked Oct 20 '25 02:10

Kianoush


2 Answers

i add this using in namespace and it solved by this :

using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
like image 145
Kianoush Avatar answered Oct 24 '25 07:10

Kianoush


Add using Microsoft.Extensions.Configuration

like image 44
Alex Avatar answered Oct 24 '25 06:10

Alex