IDX10603: Algorithm: "HS256" requires SecurityKey.KeySize to have more bits "128". KeySize reported: "32". Parameter Name: key.KeySize

I just worked with the Asp.Net Core API and implemented authentication. And I am calling this API from an Angular application. But I always get an error as shown below.

IDX10603: Algorithm: "HS256" requires SecurityKey.KeySize to have more bits "128". KeySize reported: "32". Parameter Name: key.KeySize

Below is my code for ConfigureServicesin the 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;
            }

And I use JwtPackagein 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 };
        }

Could you help me solve this problem? Thank.

+4
source share
1 answer

Ah, that was my mistake, simple. I did not provide enough characters for the secret key name.

I changed my signinkey to this one

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

from

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

, HmacSha256 SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256) 128 .

+9

Source: https://habr.com/ru/post/1689227/


All Articles