問題描述
我只是在使用 Asp.Net Core Web API 并實(shí)現(xiàn)身份驗(yàn)證.我正在從 Angular 應(yīng)用程序調(diào)用這個(gè) API.但我總是收到如下錯(cuò)誤.
I was just working with Asp.Net Core Web API, and implementing Authentication. And I am calling this API from an Angular Application. But I am always getting an error as below.
IDX10603:算法:HS256"要求 SecurityKey.KeySize 大于128"位.KeySize 報(bào)告:'32'.參數(shù)名稱:key.KeySize
IDX10603: The algorithm: 'HS256' requires the SecurityKey.KeySize to be greater than '128' bits. KeySize reported: '32'. Parameter name: key.KeySize
以下是我在 Startup.cs 文件中的 ConfigureServices
代碼.
Below is my code for ConfigureServices
in Startup.cs file.
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;
}
我在我的控制器中使用 JwtPackage
,如下所示.
And I am using JwtPackage
in my controller as follows.
JwtPackage CreateJwtToken(User usr)
{
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication"));
var signInCredentials = new SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256);
var claims = new Claim[] {
new Claim(JwtRegisteredClaimNames.Sub,usr.Id)
};
var jwt = new JwtSecurityToken(claims: claims, signingCredentials: signInCredentials);
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
return new JwtPackage() { FirstName = usr.FirstName, Token = encodedJwt };
}
你能幫我解決這個(gè)問題嗎?謝謝.
Can you please help me to fix this issue? Thank you.
推薦答案
啊,這是我的錯(cuò)誤,一個(gè)簡單的錯(cuò)誤.我沒有為密鑰名稱提供足夠的字符.
Ah, it was my mistake, a simple one. I was not providing enough characters for the secret key name.
我把我的登錄密鑰改成了這個(gè),
I changed my signinkey to this one,
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication"));
來自,
var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret phase"));
這解決了我的問題,因?yàn)?SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)
行中的 HmacSha256
應(yīng)該大于 128 位.總之,只要用一個(gè)長字符串作為key就行了.
That solved my issue, as the HmacSha256
in the line SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)
should be greater than 128 bits. In short, just use a long string as the key.
這篇關(guān)于IDX10603:算法:“HS256"要求 SecurityKey.KeySize 大于“128"位.KeySize 報(bào)告:'32'.參數(shù)名稱:key.KeySize的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!