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((“SessionID: ” & 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
“///////////
“///////////
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.