Home > OS >  ASP.NET Core 6 session get returns null
ASP.NET Core 6 session get returns null

Time:01-21

I am using ASP.NET Core 6 with razor pages.

I can set the session like the code below but when I stop and run the project, the session get is returning null, but if I set the session again in a post request through a form and button it appears again!

I changed the options in program.cs but nothing seems to be working.

I am confused about this problem! I have an example project in .NET 3 and it keeps the session but mine is not working

This is how I set the session:

HttpContext.Session.SetInt32(Constants.ShoppingCart, count);

And I get it like this in my Layout.cs:

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor HttpContextAccessor
@if (HttpContextAccessor.HttpContext.Session.GetInt32(Constants.ShoppingCart) != null)
{
    <a href="~/cart/Index" >
        @{
            var count = HttpContextAccessor.HttpContext.Session.GetInt32(Constants.ShoppingCart);
        }

        <i ></i>
        <span>@count items in cart</span>
    </a>
}

My program.cs file:

using AspDotNetApps.Core.Middleware;
using AutoMapper;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost
   .CaptureStartupErrors(true)
   .UseSetting("detailedErrors", "true");

builder.Services.AddControllersWithViews()
                .AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

var keysDirectoryName = "mydir";

var keysDirectoryPath = Path.Combine(builder.Environment.ContentRootPath, keysDirectoryName);
if (!Directory.Exists(keysDirectoryPath))
{
    Directory.CreateDirectory(keysDirectoryPath);
}
builder.Services.AddDataProtection()
  .PersistKeysToFileSystem(new DirectoryInfo(keysDirectoryPath))
  .SetApplicationName("store")
  .SetDefaultKeyLifetime(TimeSpan.FromDays(30));


builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy(Constants.AdminRole, policy => policy.RequireRole(Constants.AdminRole));
});

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
    options.Cookie.Name = ".JewelryStore.Session";
    options.IdleTimeout = TimeSpan.FromHours(24);
    options.Cookie.IsEssential = true;
    options.Cookie.HttpOnly = true;
});

builder.Services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => false;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

builder.Services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(2);
    options.SlidingExpiration = true;
    options.LoginPath = $"/Admin/Login";
    options.LogoutPath = $"/Admin/Logout";
    options.AccessDeniedPath = $"/Areas/Admin/AccessDenied";
});

builder.Services.AddRazorPages();

builder.Services.AddCors(options => options.AddPolicy("Cors", builder =>
{
    builder
    .AllowAnyHeader()
    .AllowAnyMethod()
    .WithHeaders("accept", "content-type", "origin")
    .AllowAnyOrigin();
}));



builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddTransient<IEmailService, EmailService>();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


 
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
Mapper.Initialize(x =>
{
    x.AddProfile<MappingProfile>();
});


builder.Services.AddMvc()
             .AddRazorPagesOptions(options =>
             {
                 options.Conventions.AuthorizePage("/Admin", "Main");
             });

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var context = services.GetRequiredService<ApplicationDbContext>();
     context.Database.Migrate();
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error/500");
    app.UseStatusCodePagesWithRedirects("/Error/{0}");
    app.UseHsts();
}
app.UseCors("Cors");
app.UseSecurityHeadersMiddleware(
      new SecurityHeadersBuilder()
     .AddDefaultSecurePolicy());

app.UseHttpsRedirection();
app.UseStaticFiles();
 

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();
 
app.UseSession();

app.MapRazorPages();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
    // redirects the admin when using /admin route to the admin area
    endpoints.MapAreaControllerRoute(
        "admin",
        "admin",
        "Admin/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(
        "default", "{controller=Home}/{action=Index}/{id?}");

});
 
app.Run();

CodePudding user response:

I can set the session like the code below but when I stop and run the project, the session get is returning null, but if I set the session again in a post request through a form and button it appears again!I am confused about this problem! I have an example project in .NET 3 and it keeps the session but mine is not working

Well, I think you are certainly misunderstanding between enter image description here

Note: You should see above infomration while you navigate to your browser application console.

enter image description here

Note: Your implementation and the behavior are correct in regards of session as it wouldn't keep anyting once the server or app restarted. However, if you would like to keep some value and want to retrieve it from browser side, you must need to use cookie. If you will need more information, please have a look on our official document here

  • Related