Home > Software engineering >  Sessions in .net6 after upgrading from .net4.8
Sessions in .net6 after upgrading from .net4.8

Time:12-11

I just upgraded my .net 4.8 MVC web app to .net6. I used Sessions to store objects. for example the User class:

public class User
{
    [Key]
    public string UserID { get; set; }
    public string TenantId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MobilePhone { get; set; }
    public bool IsEnabled { get; set; }
    public virtual ICollection<Department> Departments { get; set; }

}

This is how I set the session:

Session[Consts.CURRENTUSER] = userFromDb;

This is how I use it:

User _currentUser = Session[Consts.CURRENTUSER] as User;

Now, after the upgrade it does not compile. I get the following error:

Error CS0103 The name 'Session' does not exist in the current context

If i use the following HttpContext.Session[Consts.CURRENTUSER] as User it still does not allow the the above use.

Will appreciate an example on how I will be able to use the above scenario in .net core.

CodePudding user response:

after reading Microsoft docs, I followed this guide from step 4 to allow the usage of Sessions. Then, in order to use complex objects in .net core I followed this link which provided an extension method that implementing the use of complex objects in sessions.

  • Related