Mr Coder
Mr Coder

Reputation: 847

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"]));

Upvotes: 4

Views: 5529

Answers (2)

Alex
Alex

Reputation: 51

Add using Microsoft.Extensions.Configuration

Upvotes: 5

Mr Coder
Mr Coder

Reputation: 847

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

using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;

Upvotes: 9

Related Questions