[Route("api/[controller]")]
public class AuthenticateController : Controller
{
[HttpPost]
[Route("login")]
public IActionResult Login([FromBody]LoginInput input)
{
//从数据库验证用户名,密码
//验证通过 否则 返回Unauthorized
//创建claim
var authClaims = new[] {
new Claim(JwtRegisteredClaimNames.Sub,input.Username),
new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString())
};
IdentityModelEventSource.ShowPII = true;
//签名秘钥 可以放到json文件中
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecureKeySecureKeySecureKeySecureKeySecureKeySecureKey"));
var token = new JwtSecurityToken(
issuer: "https://www.cnblogs.com/chengtian",
audience: "https://www.cnblogs.com/chengtian",
expires: DateTime.Now.AddHours(2),
claims: authClaims,
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
);
//返回token和过期时间
return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token),
expiration = token.ValidTo
});
}
}