持续性Cookie和绝对过期时间
您可能希望通过浏览器会话使cookie过期。也许你也想通过绝对过期时间和认证来结束cookie,那么你可以在登录认证和创建Cookie时使用HttpContext.Authentication.SignInAsync方法中的AuthenticationProperties参数类实现。AuthenticationProperties类在Microsoft.AspNetCore.Http.Authentication命名空间中。
例如
await HttpContext.Authentication.SignInAsync(
"MyCookieMiddlewareInstance",
principal,
new AuthenticationProperties
{
IsPersistent = true
});
这个代码片段将会实现创建一个认证和相应的Cookie来实现即时浏览器关闭Cookie也能继续保留。任何在cookie属性中的过期时间的设置都将会保存下来。如果浏览器关闭时Cookie也过期了那么在重新启动浏览器是Cookie将会别清理。
await HttpContext.Authentication.SignInAsync(
"MyCookieMiddlewareInstance",
principal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
});
这段代码将创建一个身份认证和相应的cookie且将持续20分钟。 任何在Cookie options中配置的动态选项都会被忽略。 ExpiresUtc 和 IsPersistent 这两个属性是相互独立的。
其实上面bb了那么多,都没用! 不如来个demo
// 1. 在Startup.cs的Configure方法中加上
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "UserAuth", // Cookie 验证方案名称,在写cookie时会用到。
AutomaticAuthenticate = true, // 是否自动启用验证,如果不启用,则即便客服端传输了Cookie信息,服务端也不会主动解析。除了明确配置了 [Authorize(ActiveAuthenticationSchemes = "上面的方案名")] 属性的地方,才会解析,此功能一般用在需要在同一应用中启用多种验证方案的时候。比如分Area.
LoginPath = "/User/Index" // 登录页
});
// 2. 新建UserController
// 3. 创建一个测试登录的方法(这里为了方便测是我用的是get方法,方便传参请求)
public IActionResult Login(int userId, string userName)
{
WriteUser(userId, userName);
return Content("Write");
}
private async void WriteUser(int userId, string userName)
{
var identity = new ClaimsIdentity("Forms"); // 指定身份认证类型
identity.AddClaim(new Claim(ClaimTypes.Sid, userId.ToString())); // 用户Id
identity.AddClaim(new Claim(ClaimTypes.Name, userName)); // 用户名称
var principal = new ClaimsPrincipal(identity);
await HttpContext.Authentication.SignInAsync("UserAuth", principal, new AuthenticationProperties { IsPersistent = true , ExpiresUtc = DateTime.UtcNow.AddMinutes(20) }); //过期时间20分钟
}
// 4. 创建一个退出登录的方法
public async Task<ActionResult> Logout()
{
await HttpContext.Authentication.SignOutAsync("UserAuth"); // Startup.cs中配置的验证方案名
return RedirectToAction("User", "Index");
}
// 5. 创建一个获取cookie用户信息的方法方便调用
private int GetUserId()
{
//var userName = User.Identity.Name; //获取登录时存储的用户名称
var userId = User.FindFirst(ClaimTypes.Sid).Value; // 获取登录时存储的Id
if (string.IsNullOrEmpty(userId))
{
return 0;
}
else
{
return int.Parse(userId);
}
}
// 或者写一个测试Action
public JsonResult CheckLogin()
{
var userName = User.Identity.Name; //获取登录时存储的用户名称
var userId = User.FindFirst(ClaimTypes.Sid).Value; // 获取登录时存储的Id
return Json({UserId:userId,UserName:userName});
}
// 6. 以上是加密的方式如果直接写好像也是可以的
HttpContext.Response.Cookies.Append("Key", "Value");








