Session 如何存储身份验证Cookie?dotnet core 3.1中的会话
在dotnetcore中存储身份验证cookie的最佳实践是什么? 由于我的应用程序基于身份验证cookie(在cookie中存储用户sid、名称和角色),我希望在其中使用会话,因为如果用户禁用了cookie,会发生什么情况(因为我的应用程序基于cookie身份验证,因此它将不适用于该客户端)? 我们如何在asp.net核心中使用会话 登录后,我使用以下代码制作cookie:Session 如何存储身份验证Cookie?dotnet core 3.1中的会话,session,.net-core,cookies,Session,.net Core,Cookies,在dotnetcore中存储身份验证cookie的最佳实践是什么? 由于我的应用程序基于身份验证cookie(在cookie中存储用户sid、名称和角色),我希望在其中使用会话,因为如果用户禁用了cookie,会发生什么情况(因为我的应用程序基于cookie身份验证,因此它将不适用于该客户端)? 我们如何在asp.net核心中使用会话 登录后,我使用以下代码制作cookie: public IActionResult Login([FromForm] User login) {
public IActionResult Login([FromForm] User login)
{
IActionResult response = Unauthorized();
var user = AuthenticateUser(login);
if (user != null)
{
var tokenString = GenerateJSONWebToken(user);
HttpContext.Response.Cookies.Append("gamifieldAuthToken",
tokenString,
new Microsoft.AspNetCore.Http.CookieOptions()
{
Path = "/"
});
}
使用以下代码进行jwt身份验证:
private string GenerateJSONWebToken(User user)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"], new[] {
new Claim(ClaimTypes.Name, user.Username),
new Claim(ClaimTypes.Role,user.Role),
new Claim(ClaimTypes.Sid,user.Id.ToString()),
new Claim("StudentLastLogin","")
},
expires: DateTime.Now.AddMinutes(120),
signingCredentials: credentials);
var securityToken = new JwtSecurityTokenHandler().WriteToken(token).ToString();
//User.Claims.Append(new Claim(ClaimTypes.UserData, securityToken));
//((System.Security.Claims.ClaimsIdentity)Thread.CurrentPrincipal.Identity).Claims.Append(new Claim(ClaimTypes.UserData, securityToken));
return securityToken;
}
帮我一把,先谢谢你