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

Sib*_*enu 16 c# jwt asp.net-core

我只是使用Asp.Net Core Web API,并实现身份验证.我从Angular应用程序调用此API.但我总是得到如下错误.

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

下面是我ConfigureServicesStartup.cs文件中的代码.

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;
            }
Run Code Online (Sandbox Code Playgroud)

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 };
        }
Run Code Online (Sandbox Code Playgroud)

你能帮我解决这个问题吗?谢谢.

Sib*_*enu 38

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

我改变了我的签名,

var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is my custom Secret key for authnetication"));
Run Code Online (Sandbox Code Playgroud)

从,

var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Secret phase"));
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题,因为该HmacSha256SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)应该大于128位.

  • 当使用 ASCII UTF8 时,每个字符将占用 1 个字节。因此您至少需要 **16 个字符** (16x8 = 128)。 (6认同)

小智 9

我必须使用 UTF8 和 HmacSha256 来使用 40 个字符。然后它对我有用。