Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I had to block the same user multi login to my website. With the following function I am able to check and block if the same user tries to login again by using a public datatable.
Now for dealing with sessions ended without pressing logout button, can we check if a session is active based upon a user's IP address, and session ID which may be stored in a cookie?

 Public dtExistingUsers As DataTable


Protected Function fnMultipleUserCheck(ByVal UserID)
        fnMultipleUserCheck = 0

	If IsNothing(dtExistingUsers) Then
            dtExistingUsers = New DataTable
            dtExistingUsers.Columns.Add("Session_UserName")
            dtExistingUsers.Columns.Add("Session_IP")
            dtExistingUsers.Columns.Add("Session_ID")
        End If

	For Each row As DataRow In dtExistingUsers.Rows
            If row.Item("Session_UserName") = UserID Then
                fnMultipleUserCheck = 1
            End If
        Next

	If fnMultipleUserCheck = 0 Then
	    Dim newrow As DataRow = dtExistingUsers.NewRow
            newrow.Item("Session_UserName") = UserID
            newrow.Item("Session_IP") = System.Web.HttpContext.Current.Request.UserHostAddress
            newrow.Item("Session_ID") = HttpContext.Current.Session.SessionID
            dtExistingUsers.Rows.Add(newrow)
	End If

	 Return fnMultipleUserCheck
    End Function


Thanks
Posted
Updated 31-Jul-15 16:36pm
v3

1 solution

The web is stateless, a website doesn't know who is "connected" to it or where from, or who is "active". It is better to accept and go with that concept than trying to make it stateful, which many before you have tried and all have failed. Let people log in as many times as they want and don't rely on session end events, don't care about who is active and who isn't.
 
Share this answer
 
Comments
atul sharma 5126 6-Aug-15 5:43am    
A very wise suggestion. Thanks !

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