Hello friends, I am using asp net core and I want to save data in my session variables, I have been trying this for a while, but it marks an error in Session
The name 'Session' does not exist in the current context
this is my method.
public IActionResult LoginPost(LoginInfo paramsForm)
{
var mailx = paramsForm.email;
var passx = paramsForm.pass;
//Session["email"] = mailx;
// HttpContext.Session.SetString("email_user", mailx);
Session["Email"] = mailx; // <- aquí aparece
return View("Test");
}
startup
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
If we analyze the documentation
Application and session state in ASP.NET Core
in the title
Configurar el estado de sesión
you will see that you have to enable the use ofsession
To retrieve the values of
Session
you must use the extension methods specified inISession
. See here .Example
You can use the session property in these conditions:
In your case the code should be like this: