I'm trying to set a session, but it always gets null, see:
public class AutenticacaoModel : PageModelBase
{
public void OnGet()
{
HttpContext.Session.SetString("Teste", "Quero ver esse valor");
}
{
public class IndexModel : PageModelBase
{
public void OnGet()
{
Response.WriteAsync(HttpContext.Session.GetString("Teste"));
}
}
The value always comes null, and gives error.
See my 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 => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMemoryCache();
services.AddSession(options =>
{
options.Cookie = new Microsoft.AspNetCore.Http.CookieBuilder()
{
Name = "SINUCABRASIL.SESSION",
Expiration = TimeSpan.FromMinutes(30)
};
});
services
.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions
.AddPageRoute("/Autenticacao", "usuario/autenticacao")
.AddPageRoute("/MeuPerfil", "usuario/perfil")
.AddPageRoute("/MeusLocais", "usuario/estabelecimentos")
.AddPageRoute("/MeusTorneios", "usuario/eventos")
.AddPageRoute("/MinhasInscricoes", "usuario/inscricoes")
.AddPageRoute("/PoliticaPrivacidade", "privacidade/politica-de-privacidade")
.AddPageRoute("/TermosUso", "site/termos-de-uso")
.AddPageRoute("/Sobre", "site/sobre")
.AddPageRoute("/OndeJogar", "estabelecimentos")
.AddPageRoute("/Evento", "eventos")
.AddPageRoute("/Ranking", "rankings")
.AddPageRoute("/Jogador", "jogadores")
.AddPageRoute("/Contato", "suporte/fale-conosco")
.AddPageRoute("/Faq", "suporte/faq")
.AddPageRoute("/Configuracoes", "configuracoes");
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseMvc();
}