2017-11-14 10 views
3

방금 ​​Asp.Net 핵심 웹 API로 작업하고 인증을 구현했습니다. 그리고 Angular Application에서이 API를 호출합니다. 하지만 나는 항상 아래와 같은 오류가 발생합니다.IDX10603 : 알고리즘 : 'HS256'은 SecurityKey.KeySize가 '128'비트보다 커야합니다. KeySize보고 : '32'. 매개 변수 이름 : key.KeySize

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

이 문제를 해결할 수 있도록 도와주세요. 고맙습니다.

답변

7

아, 그건 내 실수 였어, 단순한거야. 비밀 키 이름에 충분한 문자를 제공하지 않았습니다. I이 한 내 signinkey 변경

에서

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

라인 SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)HmacSha256로 내 문제를 해결

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

128 비트보다 커야한다.