Question

Locked

Preventing Multiple Login Attempt in ASP.NET 3.5

By shahbaz.maqsood ·
Hi
I need some help.I need to restrict already logged In user in my website.
I have used the following way to implement this.
In global.asax.vb:
"/////////////////
Protected Sub Application_PreRequestHandlerExecute(ByVal sender As [Object], ByVal e As EventArgs)
' Let's write a message to show this got fired---
Response.Write(("SessionI " & Session.SessionID.ToString() & "User key: ") + DirectCast(Session("user"), String))
If Session("user") IsNot Nothing Then
' e.g. this is after an initial logon
Dim sKey As String = DirectCast(Session("user"), String)
' Accessing the Cache Item extends the Sliding Expiration automatically
Dim sUser As String = DirectCast(HttpContext.Current.Cache(sKey), String)
End If
End Sub
" /////////////
In WEB.CONFIG
"///////////
<sessionState
mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="server=192.168.1.111;user id=root;database=master;Password=root"
cookieless="false"
timeout="1"
/>
"///////////

In LOGIN.ASPX:
"//////////
'validate your user here (Forms Auth or Database, for example)
' this could be a new "illegal" logon, so we need to check
' if these credentials are already in the Cache
Dim sKey As String = Login1.UserName + Login1.Password
Dim sUser As String = Convert.ToString(Cache(sKey))
If sUser Is Nothing OrElse sUser = [String].Empty Then
' No Cache item, so sesion is either expired or user is new sign-on
' Set the cache item and Session hit-test for this user---
Dim SessTimeOut As New TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0)
HttpContext.Current.Cache.Insert(sKey, sKey, Nothing, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, _
Nothing)
Session("user") = Login1.UserName + Login1.Password
' Let them in - redirect to main page, etc.
Response.Write("WelCome")
e.Authenticated = True
Else
' cache item exists, so too bad...
Response.Write("Invalid Login Attempt")
Return
End If
"/////////

I got this way from web.Now i'm getting the following Exception:
"
Session state is not available in this context.
HTTPEXCEPTION was unhandled by user code."


Can anyone lead the wayout.
Waiting for Reply

Shahbaz Maqsood.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Share your knowledge
Back to Windows Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums