Rida Iftikhar
Rida Iftikhar

Reputation: 186

How to know an OWIN cookie/User Session is about to expire in mvc 5

I'n my MVC 5 application I need to prevent users to login if they are already logged in on another system. For this I have a flag column in the database and set its value to true when user logs in and false when the user logs out. But the issue occurs when the user is inactive for a certain period of time or closes the browser, which invalidates the authentication cookie but does not update the flag in the database, which in turn keeps the user logged in in the database and therefore the user will never be able to login again. I wanted to know if there is any functionality that identity framework provides where I can check that when the authentication cookie if about to expire, I can set the flag in the database to false.

Upvotes: 1

Views: 736

Answers (1)

Rida Iftikhar
Rida Iftikhar

Reputation: 186

Since I didn't get any answer here I found a work around for this and hope that will be of help to others too. What I'm doing now is storing the SessionId of the current user in the database along with setting its isLoggedIn flag to true when user logs in. And when the user logs out I set the isLoggedIn flag to false and also remove the SessionId from the database. The same functionality of logout function is also present in the Session_End() event in the Global.asax, which handles the inactivity issue. For now this is working fine for me but I'm open to other ideas if anyone has a different and better approach .

Upvotes: 1

Related Questions