我需要使用jwt创建一个登录名。当我使用此代码时:
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);
}
它向我显示此错误:
严重性代码描述项目文件行抑制状态 错误CS0021无法将带有[]的索引应用于类型为'IConfiguration'的表达式StoreFinal C:\ Users \ Mr-Programer \ Desktop \ New folder \ StoreFinal \ StoreFinal \ Areas \ Admin \ Controllers \ LoginController.cs 65活动
在此行显示错误:var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
答案 0 :(得分:2)
添加using Microsoft.Extensions.Configuration
答案 1 :(得分:1)
我在命名空间中添加它,并通过以下方法解决了这个问题:
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;