C# IDX10603:算法:';HS256';要求SecurityKey.KeySize大于';128';位。键大小报告:';32';。参数名称:key.KeySize
我只是在使用Asp.Net核心Web API,并实现身份验证。我从一个角度应用程序调用这个API。但我总是得到一个错误如下 IDX10603:算法:“HS256”要求SecurityKey.KeySize大于“128”位。键大小报告:“32”。参数名称:key.KeySize 下面是我在Startup.cs文件中的C# IDX10603:算法:';HS256';要求SecurityKey.KeySize大于';128';位。键大小报告:';32';。参数名称:key.KeySize,c#,asp.net-core,jwt,C#,Asp.net Core,Jwt,我只是在使用Asp.Net核心Web API,并实现身份验证。我从一个角度应用程序调用这个API。但我总是得到一个错误如下 IDX10603:算法:“HS256”要求SecurityKey.KeySize大于“128”位。键大小报告:“32”。参数名称:key.KeySize 下面是我在Startup.cs文件中的ConfigureServices代码 public IServiceProvider ConfigureServices(IServiceCollection services)
ConfigureServices
代码
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDbContext<APIContext>(option => option.UseInMemoryDatabase("AngularApp"));
services.AddCors(options => options.AddPolicy("Cors", builder =>
{
builder.AllowAnyOrigin().
AllowAnyMethod().
AllowAnyHeader();
}
));
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret phase"));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
IssuerSigningKey = signinKey,
ValidateAudience = false,
ValidateIssuer = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = true,
ValidateActor = false,
ClockSkew = TimeSpan.Zero
};
});
services.AddMvc();
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
你能帮我解决这个问题吗?谢谢。啊,这是我的错误,很简单。我没有为密钥名称提供足够的字符 我把我的签名改成了这个
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication"));
从
这解决了我的问题,因为
签名凭证(signinKey,SecurityAlgorithms.HmacSha256)
行中的HmacSha256
应该大于128位。简而言之,只需使用一个长字符串作为键。谢谢。这很有帮助。当使用ASCII UTF8时,每个字符将有1个字节。因此,您至少需要16个字符(16x8=128)。
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication"));
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret phase"));