简体   繁体   中英

Sessions in .net6 after upgrading from .net4.8

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM