방금 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 };
}
이 문제를 해결할 수 있도록 도와주세요. 고맙습니다.