1.Session
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSession();
}
app.UseSession();
public IActionResult Index() { var user = base.HttpContext.Session.GetString("CurrentUser"); if (string.IsNullOrEmpty(user)) { base.HttpContext.Session.SetString("CurrentUser", DateTime.Now.ToString()); } base.ViewBag.User = base.HttpContext.Session.GetString("CurrentUser"); return View(); }
刷新后,时间是固定的