IDX10603:该算法:“HS256”要求安全密钥的密钥大小大于“128”位。报告的密钥大小为:“32”。参数名称:key.KeySize。

80

我刚刚在使用Asp.Net Core Web API,并实现身份验证。 我正在从Angular应用程序调用此API。 但是,我总是收到以下错误。

IDX10603: 算法:'HS256'要求SecurityKey.KeySize大于'128'位。 KeySize报告为:'32'。 参数名称:key.KeySize

下面是在Startup.cs文件中的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;
            }

我在控制器中使用JwtPackage,如下所示。

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 };
        }

你能帮我修复这个问题吗?谢谢。

3个回答

181

啊,这是我的错误,一个简单的错误。我没有为密钥名称提供足够的字符。

我将我的签到密钥更改为以下内容:

var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authentication"));

来自,

var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret phase"));

问题解决了,因为在代码行 SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)中,HmacSha256应该大于128位。简而言之,只需使用一个长字符串作为密钥。


4
当使用ASCII时,UTF8每个字符将给您1个字节。因此,您至少需要16个字符(16x8 = 128)。 - pmoleri
我之前一直收到“找不到键”错误的提示,但实际上该键是存在的。后来发现这个错误其实很误导人——原来该键实在是太短了。 - Maxim Zabolotskikh

6

我必须使用UTF8和HmacSha256来使用40个字符,然后它对我起作用了。


3
打开 appsettings.json 文件,然后在 "SiteSettings" 或 "AppSettings" 中找到你命名为 "SecretKey" 或 "JwtSecret" 的字符串,或者根据我附在评论中的图片来确定。新的字符串长度必须比当前字符串长,并且大约为70个字符。

Appsettings1

Appsettings2


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接