简体   繁体   中英

how to get value from session in Razor View?

in Core 2.1: I set the value in controller

HttpContext.Session.SetString("IsAuthenticated", "true");

when i get the value in view.cshtml, the value is null

if (Context.Session.GetString("IsAuthenticated") == "true")

I just ran your code in an app I have and it worked. It would seem to me that you haven't set up the use of cookies in your app. Try the following:

// The following goes into ConfigureServices in Startup.cs
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromMinutes(5);
    options.Cookie.HttpOnly = true;
});

Having done that, you'll still need one more thing to set it up in the Configure method in Startup.cs

// place after the other app.UseFoo methods. 
app.UseSession(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM