Reputation: 847
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
Reputation: 847
i add this using in namespace and it solved by this :
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
Upvotes: 9