views:

175

answers:

2

Hello,

I have an experience with CakePHP and now started coding on ASP.NET MVC framework. I have a problem with the login system. How can I restrict users from logging only one time simultaneously on my system?

I can create a field in my DB where Customer becomes active when logs in. If he logs out I can make active false. But what if the session just ends? How can I catch this?

A: 

This is, unfortunately, something of a challenge due to the way that the session end event are implemented as you don't have access to the information you need when they fire.

So turn the problem on its head a little, if you track the session that they last logged in on then if you get a request from that same authenticated user in a different session then remove the auth for that session (in effect the older session) with an appropriate redirect to a suitable message.

The key here is tracking not only who is currently logged in but also the session ID for that login.

Details are a bit more complicated - but you can perform the test at a request level or by adding your own base page class, deriving all your "real" pages from that and checking in a page event.

Murph
I can keep the sessionID in my database. If user logs out it is deleted. But if user's session ends the sessionID stays there.So when user logs in I must check whether current sessionId is different then the one in database, and whether session timeout has ended, asuming I also keep user's loging datetime, correct?
gong
Hmm, sort of. There isn't a single solution. The way I was looking at it was that when a user logs in you don't particularly care what session was live before because that session is now invalid and the user (of the "old" session) needs to be forceably logged out. What you care about is whether the combination of user *and* session is valid when you make a request.
Murph
+1  A: 

This article provides a possible solution.

pmarflee
You get an upvote 'cos that's doing something similar to my suggestion and has code!
Murph