Click here to Skip to main content
15,880,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I have Create a Web application using ASP.NET, C#, SQL-Server. and Hosted in IIS server.Its working fine.

I need to avoid multiple logins with Same Username.
The User should not log in with same username from other systems.

It should Display a message Already Logged in.

Please Help me this problem. SO WHAT ARE THE STEPS TO DO THIS ? ?

Thanks.
Posted
Comments
RDBurmon 13-Jun-12 9:28am    
Thanks Everyone who replied to this thread , So Ranjith, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.
viral panwala 30-Aug-17 2:39am    
user can Close browser at that how it will be work ???

I think the easiest way to do this is by managing a list of logged in users in Application variable. since this variable is available to all users and even for not logged in user.

1. add the list of logged in user in application variable, initially empty.
2. when someone logs in check if his name is in the list.
3. if not then let him log in and then add his name to the list.
4. when someone logs off remove the name from the list.

so your scenario will be handled in 2nd point when if user is logged in he will not be allowed to log in again.

Note: This is assuming that you have only one server and not web farm.

If you have a web farm then you need to do this in database. the algo will then be

1. add the list of logged in user in a db table, initially empty.
2. when someone logs in check if his name is in the list.
3. if not then let him log in and then add his name to the list.
4. when someone logs off remove the name from the list.
 
Share this answer
 
Comments
Anand Chavda 23-Oct-13 5:52am    
But what if user accidently closed the browser?The entry will not get deleted from the database
Hai,

The using of table is failure in case of user closes the browser unfortunately.

As you know,Cache is an application specific object not user specific and this can expirable after the specified time which can set by the user.

The best of approach is use the cache class and add used ID to cache with minimal expired time(say 2 minutes).Also put user ID and session ID into session variable.

Write the code in Application_PreRequestHandlerExecute event in global.asax file.

At each hit, read the session ID from session variable and check this session ID is match with session.sessionID value.If not match redirect the user to logout page.(Since the user already logged in some other session)

If match then check the user ID from cache.If it is null then again set the user ID(Read from session variable) into cache with same expirable time(say 2 minutes).

Due to this approach,if the user is idle beyond cache expiry time (say 2 minutes),the same user can login in some other session.

By checking the user ID from cache during login we can avoid multi-login.

Thanks
Mani
 
Share this answer
 
Hi,

Take one more column in your database table as bit. By default set it to 0(Means not logged in). If a user logs in just check for the bit, If it is 0 means user can log in(after logging in set it to 1 and after log out again set it to 0) otherwise user cannot log in.
Else you can use application management also to prevent the user from multiple login with same userid and password.
In the case of session timeout you can fire the events using delegates. Have a look Here[^].


This may help you.
All the best.
--AK
 
Share this answer
 
v3
Comments
Ranjith Reddy CSE 13-Jun-12 2:45am    
Already, I have did this in Database, But what about the Coding.
Rahul.KumarSharma 13-Jun-12 2:49am    
Also when session Time out occur than update this bit to 0.
[no name] 13-Jun-12 2:57am    
In the case of session timeout you can fire the event's. right?
Check out my updated answer.

[no name] 13-Jun-12 3:02am    
If you are still having problem in maintaining all these things then go for application management..
stib_markc 13-Jun-12 4:30am    
Simple solution, 5!
hi,
Well I am not going to write a code for you here but I will guide you to overcome your issue. I had similar issue when I was working on a project. I then created a table in database named LogedInUsers with UserID as foreign key there. On the login page add an entry in this table for the current user and delete it on sign out.

Before the login check logic just add a check in the LogedInUsers table if this UserID is exist in this table or not, if exist then show a message to user that 'This user is already loged in.'
SQL
where UserID not in (select UserID from LogedInUsers )


Hope you get an idea and able to solve your problem.
 
Share this answer
 
Comments
Mizanur Rahman 2023 20-Jul-23 5:01am    
but if user not logged out from browser , just close the browser then ? if it not hot to logout then what happen ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900