Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 创建Web令牌问题_Asp.net Core_Jwt_Asp.net Core 2.0 - Fatal编程技术网

Asp.net core 创建Web令牌问题

Asp.net core 创建Web令牌问题,asp.net-core,jwt,asp.net-core-2.0,Asp.net Core,Jwt,Asp.net Core 2.0,我正在尝试在.NETCore2WebAPI应用程序中创建并使用JWT进行授权。此行产生以下粗体错误: public string Value => new JwtSecurityTokenHandler().WriteToken(this.token); System.ArgumentOutOfRangeException:'IDX10603:算法:'HS256' 要求SecurityKey.KeySize大于“128”位。 报告的密钥大小为:“96” 下面是完整的代码。资料来源:

我正在尝试在.NETCore2WebAPI应用程序中创建并使用JWT进行授权。此行产生以下粗体错误:

public string Value => new JwtSecurityTokenHandler().WriteToken(this.token);
System.ArgumentOutOfRangeException:'IDX10603:算法:'HS256' 要求SecurityKey.KeySize大于“128”位。 报告的密钥大小为:“96”


下面是完整的代码。资料来源:

[AllowAnonymous]
[HttpPost,路由(“CreateToken”)]
公共IActionResult CreateToken([FromBody]RegisterMemberModel inputModel)
{
var token=new JwtTokenBuilder()
.AddSecurityKey(JwtSecurityKey.Create(“fiversecret”))
.AddSubject(“詹姆斯·邦德”)
.AddIssuer(“五款证券持有人”)
.AddAudience(“五方担保持有人”)
.AddClaim(“会员身份ID”,“111”)
.AddExpiry(1)
.Build();
返回Ok(token.Value);
}
公开密封类JwtToken
{
私有JwtSecurityToken令牌;
内部JwtToken(JwtSecurityToken令牌)
{
this.token=token;
}
public DateTime ValidTo=>token.ValidTo;
公共字符串值=>new JwtSecurityTokenHandler().WriteToken(this.token);
}
公共密封类JWT令牌生成器
{
私有SecurityKey SecurityKey=null;
私有字符串subject=“”;
私有字符串发布者=”;
私人字符串“”;
私有字典声明=新字典();
私人整数分钟=5;
公共JwtTokenBuilder AddSecurityKey(SecurityKey SecurityKey)
{
this.securityKey=securityKey;
归还这个;
}
公共JwtTokenBuilder AddSubject(字符串主题)
{
this.subject=主语;
归还这个;
}
公共JwtTokenBuilder附加发行人(字符串发行人)
{
this.issuer=发行人;
归还这个;
}
公共JwtTokenBuilder AddAudience(字符串访问者)
{
这个。观众=观众;
归还这个;
}
公共JwtTokenBuilder AddClaim(字符串类型、字符串值)
{
此.claims.Add(类型、值);
归还这个;
}
公共JwtTokenBuilder AddClaims(字典声明)
{
本.索赔.联合(索赔);
归还这个;
}
公共JwtTokenBuilder AddExpiry(整数分钟)
{
this.expiryInMinutes=expiryInMinutes;
归还这个;
}
公共JwtToken构建()
{
保证义务();
var索赔=新列表
{
新索赔(JwtRegisteredClaimNames.Sub,本主题),
新声明(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString())
}
.Union(this.claims.Select(item=>newclaims(item.Key,item.Value));
var token=新的JwtSecurityToken(
发行人:这个发行人,
观众:这个,观众,,
索赔:索赔,
expires:DateTime.UtcNow.AddMinutes(expireyMinutes),
signingCredentials:新的signingCredentials(
这个.securityKey,
SecurityAlgorithms.HmacSha256);
返回新的JwtToken(token);
}    
私人无效保证书()
{
if(this.securityKey==null)
抛出新的ArgumentNullException(“安全密钥”);
if(string.IsNullOrEmpty(this.subject))
抛出新的异常(“主题”);
if(string.IsNullOrEmpty(this.issuer))
抛出新的异常(“发行人”);
if(string.IsNullOrEmpty(this.accession))
抛出新的异常(“观众”);
}
}
Block size:底层哈希算法中数据块的大小 对……起作用。对于SHA-256,这是512位,对于SHA-384和SHA-512, 这是1024位

Output length:基础文件生成的哈希值的大小 散列算法。对于SHA-256,这是256位,对于SHA-384,这是384位 位,对于SHA-512,这是512位


因此,我们需要一个128位的密钥。如果您想将其存储为文本,则可以通过生成一个随机的32字符长度的字符串来表示128位密钥。

我也遇到了同样的问题(作者的道具或本指南)。密钥长度是个问题,可能是作者的输入错误

而不是

JwtSecurityKey.Create("fiversecret ")
使用


谢谢。不知道我需要对代码做什么更改吗?我对JWT很陌生。你是说我应该:用一个随机的32字符文本字符串替换他原始代码中的“fiversecret”文本吗?.AddSecurityKey(JwtSecurityKey.Create(“fiversecret”))我有一个repo,例如JWT身份验证,检查它的
Startup
AccountController
。非常感谢Sirwan!!非常有用。
JwtSecurityKey.Create("fiver-secret-key")