Session is null and void

Asked

Viewed 31 times

0

I’m trying to set up a session, but it always gets null, look:

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.

Check out 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();
}    

Visual Studio

  • Apparently your code is right, you went through OnGet() of AutenticacaoModel before entering the other page?

  • Opa, yes, that’s what I call: Httpcontext.session.Setstring("Test", "I want to see this value");

  • I recreated the project, and then it worked. In another project, I have the same problem.

  • Probably some reference problem, you tried on a different computer?

  • I recreated the project, and then it worked. Pretty messed up.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.