ASP.NET Core clear session issue

Plarent Haxhidauti

I have an application where I save some information on the session that later I assign to the model when I save it to the DB. For example I have the following model saved by User1:

...
MyModel model = new MyModel();
model.name = mypostedModel.name;
model.type = HttpContext.Session.GetString("UniqueTypeForThisUser");
...

After I save the model in my DB, at the end of the post method, I clear the session with this line:

HttpContext.Session.Clear();

Let's say at the same time there's a User2 creating a new model and I have saved another value in the session with a unique key for User2. Same way as before, at the end of the post method I clear the session with the Clear() method.

Does this clear session method clear the session for all users, or only for one user. If for example User1 saves the model first and clears the session for all users, then the User2 will get his session variable cleared (lost) and will assign a null value to my 'type' column for the model.

For the documentation this was not clear for me. Thanks

poke

The session object that you can access for example through HttpContext.Session is specific to a single user. Everything you do there will only affect the user that belongs to this session and there is no mix between sessions of other users.

That also means that you do not need to choose session configuration key names that are somewhat specific to a user. So instead of using GetString("UniqueTypeForThisUser"), you can just refer to the values using a general constant name:

var value1 = HttpContext.Session.GetString("Value1");
var value2 = HttpContext.Session.GetString("Value2");

Each user session will then have these values independently. As a result, calling Session.Clear() will also only clear the session storage for that session that is specific to its user.

If you actually do need different means for storing state, be sure to check out the docs on application state. For example, things that should be stored independently of the user can be stored using an in-memory cache.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

ASP .Net Core Routing issue?

From Dev

Internet Explorer 11 Session Issue with Asp.Net 4.0

From Dev

Save user session in Redis with ASP.NET Core in Azure

From Dev

How to access session from a view in ASP .NET Core MVC 1.0

From Dev

How to access the Session in ASP.NET Core via static variable?

From Dev

ASP NET Core MVC - How to configure Out of Process Session State?

From Dev

Session/ Temp data Strategy for related data with asp.net core

From Dev

Resharper compatibility issue with ASP.NET core 1.0

From Dev

ASP.Net Core Yeoman Template issue on Mac

From Dev

"Contact us" page with ASP.NET Core - SMTP server issue

From Dev

Asp.net core, Angular 2. CookieAuth issue

From Dev

Microsoft Edge issue on export HTML to PDF in ASP.NET Core

From Dev

How can I clear asp.net session value in OnUnload event in javascript

From Dev

ASP.NET don't clear cookies session when close browser

From Dev

VS 2017 RC asp.net core system.net.http confliction issue

From Java

Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to

From Dev

ASP .NET Core Cookie Authentication expiration changes from timestamp to "Session" upon return

From Dev

ASP.NET 5 (Core): How to store objects in session-cache (ISession)?

From Dev

Is session storage supported in Blazor Webassembly (PWA) standalone (Not hosted in asp.net core)?

From Dev

Storing user data in session store or retrieving from database in each request in asp.net core?

From Dev

Microsoft.AspNet.Session not available in ASP.NET core 1.0 rc1 update2

From Dev

ASP.NET Core MVC dropdown list on _layout page to a Session variable

From Dev

Handling expired Azure AD session in asp.net core and angular app

From Dev

asp net core 2.1 api jwt token Session.id changes on every request

From Dev

session variables in an ASP.NET

From Dev

session not working asp.net

From Dev

Alternative to session in ASP.NET

From Dev

asp.net Login Session

From Dev

RavenDB Dependency Issue with .NET Core

Related Related

  1. 1

    ASP .Net Core Routing issue?

  2. 2

    Internet Explorer 11 Session Issue with Asp.Net 4.0

  3. 3

    Save user session in Redis with ASP.NET Core in Azure

  4. 4

    How to access session from a view in ASP .NET Core MVC 1.0

  5. 5

    How to access the Session in ASP.NET Core via static variable?

  6. 6

    ASP NET Core MVC - How to configure Out of Process Session State?

  7. 7

    Session/ Temp data Strategy for related data with asp.net core

  8. 8

    Resharper compatibility issue with ASP.NET core 1.0

  9. 9

    ASP.Net Core Yeoman Template issue on Mac

  10. 10

    "Contact us" page with ASP.NET Core - SMTP server issue

  11. 11

    Asp.net core, Angular 2. CookieAuth issue

  12. 12

    Microsoft Edge issue on export HTML to PDF in ASP.NET Core

  13. 13

    How can I clear asp.net session value in OnUnload event in javascript

  14. 14

    ASP.NET don't clear cookies session when close browser

  15. 15

    VS 2017 RC asp.net core system.net.http confliction issue

  16. 16

    Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to

  17. 17

    ASP .NET Core Cookie Authentication expiration changes from timestamp to "Session" upon return

  18. 18

    ASP.NET 5 (Core): How to store objects in session-cache (ISession)?

  19. 19

    Is session storage supported in Blazor Webassembly (PWA) standalone (Not hosted in asp.net core)?

  20. 20

    Storing user data in session store or retrieving from database in each request in asp.net core?

  21. 21

    Microsoft.AspNet.Session not available in ASP.NET core 1.0 rc1 update2

  22. 22

    ASP.NET Core MVC dropdown list on _layout page to a Session variable

  23. 23

    Handling expired Azure AD session in asp.net core and angular app

  24. 24

    asp net core 2.1 api jwt token Session.id changes on every request

  25. 25

    session variables in an ASP.NET

  26. 26

    session not working asp.net

  27. 27

    Alternative to session in ASP.NET

  28. 28

    asp.net Login Session

  29. 29

    RavenDB Dependency Issue with .NET Core

HotTag

Archive