我使用本网站对角2/4使用.NET核心API身份验证。
注册工作正常,但我在身份验证(登录)时出现了令牌错误。服务器给出了以下错误:
HTTP500:服务器错误--服务器遇到了一个意外的情况,使得它无法完成请求。
这是我的代码:
[AllowAnonymous]
[HttpPost]
public IActionResult Authenticate([FromBody]ApplicationUserDto applicationUserDto)
{
var appUser = _appUserService.Authenticate(applicationUserDto.Username, applicationUserDto.Password);
if (appUser == null)
return Unauthorized();
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, appUser.Id)
}),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor); //Here returns the Error
var tokenString = tokenHandler.WriteToken(token);
// return basic user info (without password) and token to store client side
return Ok(new
{
Id = appUser.Id,
Username = appUser.Username,
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Token = tokenString
});
}我不知道有什么问题。我仔细地写了代码(而不是复制粘贴)使用网站。有什么问题吗?
发布于 2017-07-10 08:17:18
我已经找到并解决了错误。
我在项目中实现了应用程序洞察(.NET Core )。我又请求了一次以上的要求从角2项目。然后我去了portal.azure.com,在一个标签中打开了Application。我选择了失败的请求来查看更详细的内容。另一个例外的标题是:
System.ArgumentOutOfRangeException at MyProject.Controllers.ApplicationUsersController.Authenticate。
然后我点击它获得更多的细节,在那里我发现了问题:
IDX10603: The algorithm: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' requires the SecurityKey.KeySize to be greater than '128' bits. KeySize reported: '64'. Parameter name: key.KeySize
密钥保存在appsettings.json文件中。所以我只需要给一个更长的秘密钥匙。
现在它没问题了!
https://stackoverflow.com/questions/44997839
复制相似问题