redis实现共享session

1.安装package

redis实现共享session

 

 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数据,

redis实现共享session

 

上一篇:Mac新手使用技巧,键盘键位分布和快捷键


下一篇:教你从零开始画echarts地图