locked
Session being lost between controllers Asp.net core RRS feed

  • Question

  • User-183185495 posted

    Ok I am using a session variable to store a case Id that is linked between tables. I am using .net 3.1 I just need this simple value passed between controllers It appears to only work within the current controller.

    public void ConfigureServices(IServiceCollection services) {
     services.Configure<CookiePolicyOptions>(options =>
    {
           options.CheckConsentNeeded = context => true; // consent required
           options.MinimumSameSitePolicy = SameSiteMode.None;
         });
    
          ervices.AddSession(opts =>
          {
             opts.Cookie.IsEssential = true; // make the session cookie Essential
          });
        ...
    }

    This is the second controller where i wish to read in the above session.

        public class VesselsController : Controller
        {
            private readonly MISDBContext _context;
    
            public VesselsController(MISDBContext context) {
                _context = context;
                GetCompanies();
            }
        // POST: Vessels/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,CountryOfOrigon,CompanyId,IMONumber,Flag,Company,Country,CallSign,MMSI,VesselType,Active,isDeleted,isActive,CreatedDate,CreatedBy,MISObjectId,RelationShipId")] Vessels vessels)
        {
                if (ModelState.IsValid) {
                    var realtionShipId = Int32.TryParse(HttpContext.Session.GetString("relationShipId"), out int resultRelationshipId);
    
                    Int32.TryParse(HttpContext.Session.GetString("CaseId"), out Int32 resultCaseId);
                    vessels.isActive = true;
                    vessels.isDeleted = false;
                    vessels.CreatedBy = HttpContext.Session.GetString("Intitals");
                    vessels.LastModfiedDate = DateTime.Now;                
                    vessels.CreatedDate = DateTime.Now;
                    vessels.LastModfiedBy = HttpContext.Session.GetString("Intitals");                
                    vessels.MISObjectId = resultCaseId;
                    vessels.RelationShipId = resultRelationshipId;
                    _context.Add(vessels);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("Edit", "Relationships", new { Id = vessels.MISObjectId });
    
                }
                GetCompanies();
                return View(vessels);
            }
    } 

    Its this resultCaseId I have lost the variable and yes I have setup the configure middle ware.

         app.UseSession();

    Monday, July 13, 2020 10:11 PM

All replies

  • User2078676645 posted

    Hi,

    I tested these codes according to your situation, this will not cause the session to be lost, I do not know which controller you are in or in what way to handle the session.

    Regards,

    Evern

    Tuesday, July 14, 2020 8:47 AM
  • User-2054057000 posted

    Try setting session timeout to 1 hours and try again.

    services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan. FromHours(1); // 1 hours expiry
        });

    You can check the tutorial called Sessions in ASP.NET Core for more information.

    Tuesday, July 14, 2020 9:17 AM