How do I clear a remote session after publishing my C# MVC application from Visual Studio?

martin cooke 1 Reputation point

So I have my C# MVC application, happy with its design, I publish the application onto external servers (ionos), once I've published I've found if I create a new account or go to log into the application its like the session is still hanging around. As what actually happens is I can register as a new user, then get forwarded into the secure area of the application however because I use Knockout Js I can see the internal UI but its like I'm not really logged in if that makes sense. What I can do straight after is hit F5 I'm then logged out, I can log back in using the same credentials so the user registration has in fact worked sort of but its not the same session.

So basically I think what's happening is the session is still cached on the external servers if after I publish, there is always just one session knocking about perhaps, as what happens every now and again is a user will register and they will say something like the drop down boxes aren't populated whereas what I think has really happened is they have registered in an old session. The login will still work but this behaviour is causing a nightmare, only just managed to pin it down, its always after I have published my application.

So, after I publish how do I kill of the servers cache if that makes sense, normally I could use IIS to restart the server and this would do the job however I'm using external webservers here so I cannot access IIS? Is there some sort of publish setting perhaps? Or can I ftp onto the server and perhaps delete a cache file so after a publish this wont happen. Hope I've explained this correctly, any help/input gratefully received, thanks in advance :)

A set of technologies in the .NET Framework for building web applications and XML web services.
4,311 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,793 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,533 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. martin cooke 1 Reputation point

    so what I tried doing was using the publishing setting "delete all existing files prior to publish", this completely removes the app as you'd expect but I still get the same issue, I can log into an old session, I hit F5 am logged out and have to login again - am thinking the only real solution to this is to restart IIS. Unless anyone anywhere has any ideas?

    0 comments No comments

  2. Bruce Zhang-MSFT 3,736 Reputation points

    Hi @martin cooke ,

    One way to clear the session in IIS is to restart IIS which you said, and the other is to recycle the application pool.

    These two methods will affect the operation of IIS or the application, so the better way is to clear the session programmatically in the application.

    The methods that can clear the session are Session.Clear(), Session.RemoveAll(), Session.Abandon(). I suggest you use Session.Abandon(). Compared with the two other methods, it is not only to delete session items, the session ID will also be deleted.

    Or you can consider this problem from another angle, modify the Session state mode in IIS. The default is InProcess, which stores the data in the memory object of the worker process (aspnet_wp.exe) in the application domain. It only loses session data when IIS restarts. If the SQL Server mode is adopted, the session data will be stored in the database and deleted directly from the database without restarting IIS or App pool. As long as it is always stored in the database, the session value can always be read.

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Bruce Zhang