2

In an MVC application, a user has logged in Machine1. The same user has logged in Machine2 as well. At that time, I would like to Logout the user From Machine1 and would like to clear the session from Machine1. How to achieve this?

It would be grateful if you provide any sample application/Code.

Please do help. Thanks in advance. Please do help. Thanks in advance.

ANJYR
  • 2,583
  • 6
  • 39
  • 60
Pappu Kumar
  • 21
  • 1
  • 4

4 Answers4

0

Session.Abandon() destroys the session and the Session_OnEnd event is triggered.

Session.Clear() just removes all values (content) from the Object. The session with the same key is still alive.

So, if you use Session.Abandon(), you lose that specific session and the user will get a new session key. You could use it for example when the user logs out.

Use Session.Clear(), if you want that the user remaining in the same session (if you don't want him to relogin for example) and reset all his session specific data.

0

To achieve desired functionality first I would recommend after a user successfully logged in, save SessionId CurrentDateTime and UserId to some special table in the database for example "LoginHistory". Then in global.asax in the

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e);

pull top 1 row ordered by DateTime from "LoginHistory" for current user id and compare sessionId value from database with sessionId of the current user.

If they are different then logout current user

FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
Max Zuber
  • 19
  • 5
0

Here are some scenarios when which method will use for kill session.

Session.Remove(yoursession); -> Removes an item from the session state collection.

Session.RemoveAll() -> Removes all items from the session collection.

Session.Clear() -> Remove all items from session collection. Note: There is no difference between Clear and RemoveAll. RemoveAll() calls Clear() internally.

Session.Abandon() - > Cancels the current session.

According to your requirement, this will better

FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();

SignOut method removes the forms-authentication ticket information from the cookie or the URL if CookiesSupported is false. You can use the SignOut method in conjunction with the RedirectToLoginPage method to log one user out and allow a different user to log in.

Please refer to LINK

ANJYR
  • 2,583
  • 6
  • 39
  • 60
-1

use this in your logout Action may be it help you

  Session.Clear();

or else

 session["YourSessionKey"]=null;