【发布时间】:2017-12-13 08:23:07
【问题描述】:
我正在使用 this website 为 Angular 2/4 使用 .NET Core API 身份验证。
注册有效,但在身份验证(登录)时令牌出现错误。服务器给了我这个错误:
HTTP500: SERVER ERROR - 服务器遇到了阻止它完成请求的意外情况。
这是我的代码:
[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
});
}
我不知道是什么问题。我使用该网站仔细编写了代码(不是复制粘贴)。有什么问题?
【问题讨论】:
标签: angular authentication .net-core