<legend id='CYC5K'><style id='CYC5K'><dir id='CYC5K'><q id='CYC5K'></q></dir></style></legend>

    1. <tfoot id='CYC5K'></tfoot>
        <bdo id='CYC5K'></bdo><ul id='CYC5K'></ul>

        <small id='CYC5K'></small><noframes id='CYC5K'>

        <i id='CYC5K'><tr id='CYC5K'><dt id='CYC5K'><q id='CYC5K'><span id='CYC5K'><b id='CYC5K'><form id='CYC5K'><ins id='CYC5K'></ins><ul id='CYC5K'></ul><sub id='CYC5K'></sub></form><legend id='CYC5K'></legend><bdo id='CYC5K'><pre id='CYC5K'><center id='CYC5K'></center></pre></bdo></b><th id='CYC5K'></th></span></q></dt></tr></i><div id='CYC5K'><tfoot id='CYC5K'></tfoot><dl id='CYC5K'><fieldset id='CYC5K'></fieldset></dl></div>
      1. IDX10603:算法:“HS256"要求 SecurityKey.KeySize 大于

        时间:2023-06-01

        <tfoot id='PvYf7'></tfoot>
          <bdo id='PvYf7'></bdo><ul id='PvYf7'></ul>
        • <i id='PvYf7'><tr id='PvYf7'><dt id='PvYf7'><q id='PvYf7'><span id='PvYf7'><b id='PvYf7'><form id='PvYf7'><ins id='PvYf7'></ins><ul id='PvYf7'></ul><sub id='PvYf7'></sub></form><legend id='PvYf7'></legend><bdo id='PvYf7'><pre id='PvYf7'><center id='PvYf7'></center></pre></bdo></b><th id='PvYf7'></th></span></q></dt></tr></i><div id='PvYf7'><tfoot id='PvYf7'></tfoot><dl id='PvYf7'><fieldset id='PvYf7'></fieldset></dl></div>

            <legend id='PvYf7'><style id='PvYf7'><dir id='PvYf7'><q id='PvYf7'></q></dir></style></legend>

              <small id='PvYf7'></small><noframes id='PvYf7'>

                    <tbody id='PvYf7'></tbody>
                  本文介绍了IDX10603:算法:“HS256"要求 SecurityKey.KeySize 大于“128"位.KeySize 报告:'32'.参数名称:key.KeySize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我只是在使用 Asp.Net Core Web API 并实现身份验证.我正在从 Angular 应用程序调用这个 API.但我总是收到如下错误.

                  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 报告:'32'.参数名称: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 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 };
                          }
                  

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

                  Can you please help me to fix this issue? Thank you.

                  推荐答案

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

                  Ah, it was my mistake, a simple one. I was not providing 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"));
                  

                  来自,

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

                  这解决了我的问题,因为 SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256) 行中的 HmacSha256 应该大于 128 位.总之,只要用一个长字符串作为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.

                  这篇关于IDX10603:算法:“HS256"要求 SecurityKey.KeySize 大于“128"位.KeySize 报告:'32'.参数名称:key.KeySize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JWT 和 Web API (JwtAuthForWebAPI?) - 寻找示例 下一篇:JwtSecurityTokenHandler 和 TokenValidationParameters

                  相关文章

                  最新文章

                  • <bdo id='EwlHK'></bdo><ul id='EwlHK'></ul>

                      <tfoot id='EwlHK'></tfoot>

                    1. <legend id='EwlHK'><style id='EwlHK'><dir id='EwlHK'><q id='EwlHK'></q></dir></style></legend>
                      <i id='EwlHK'><tr id='EwlHK'><dt id='EwlHK'><q id='EwlHK'><span id='EwlHK'><b id='EwlHK'><form id='EwlHK'><ins id='EwlHK'></ins><ul id='EwlHK'></ul><sub id='EwlHK'></sub></form><legend id='EwlHK'></legend><bdo id='EwlHK'><pre id='EwlHK'><center id='EwlHK'></center></pre></bdo></b><th id='EwlHK'></th></span></q></dt></tr></i><div id='EwlHK'><tfoot id='EwlHK'></tfoot><dl id='EwlHK'><fieldset id='EwlHK'></fieldset></dl></div>
                    2. <small id='EwlHK'></small><noframes id='EwlHK'>