1.安装package
2.在startup类中
services.AddSession(options=> {
options.IdleTimeout = TimeSpan.FromMinutes(1);
});
services.AddDistributedRedisCache(option => {
option.InstanceName = "redis_session1106";
option.Configuration = "127.0.0.1:6379";
});
3.在controller类中
public IActionResult Index() { var user = base.HttpContext.Session.GetString("CurrentUser"); if (string.IsNullOrEmpty(user)) { base.HttpContext.Session.SetString("CurrentUser", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } base.ViewBag.User = base.HttpContext.Session.GetString("CurrentUser"); return View(); }
4.第一次请求后,redis中可以看到session数据,