Bromberg's Session Timeout Control

S

SAL

Hello,
I'm trying to implement Peter Bromberg's Session Timeout Control:
http://www.eggheadcafe.com/articles/20051228.asp
in ASP.NET 2.0 and am finding that the code never fires. I'm not sure what
I'm doing wrong here. Yes, it would be easy enough to place some of that
code in a class, or test for a particular Session variable but it would be
nice to have a control to drop on a web form and have it just work. Can
anyone tell me what I might be missing. Sorry, I'm still pretty new to
ASP.NET:

Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI

<DefaultProperty("Text"), _
ToolboxData("<{0}:SessionTimeoutControl
runat=server></{0}:SessionTimeoutControl>")> _
Partial Class UserControls_SessionTimeOutControl
Inherits System.Web.UI.UserControl

' reference for this control:
' http://www.eggheadcafe.com/articles/20051228.asp

#Region "Private member variables"
Private mRedirectUrl As String
#End Region

#Region "Public Properties"
<Bindable(True), Category("Appearance"), DefaultValue("")> _
Public Property RedirectUrl() As String
Get
Return mRedirectUrl
End Get
Set(ByVal value As String)
mRedirectUrl = value
End Set
End Property

Public Overrides Property Visible() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'
End Set
End Property

Public Overrides Property EnableViewState() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'MyBase.EnableViewState = value
End Set
End Property
#End Region

#Region "Protected Members"
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
If HttpContext.Current Is Nothing Then
writer.Write("[ *** SessionTimeout: " + Me.ID + " *** ]")
End If
MyBase.Render(writer)
End Sub

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
If Me.mRedirectUrl Is Nothing Then
Throw New InvalidOperationException("RedirectUrl Property Not
Set.")
End If
If Context.Session IsNot Nothing Then
Dim sCookieHeader As String = Page.Request.Headers("Cookie")
If (sCookieHeader IsNot Nothing) And
(sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) Then
If Page.Request.IsAuthenticated Then
FormsAuthentication.SignOut()
End If
Page.Response.Redirect(Me.mRedirectUrl)
End If
End If
End Sub
#End Region
End Class


SAL
 
P

Peter Bromberg [C# MVP]

SAL,
It worked fine when I wrote the article. Of course, you have translated in
to VB.NET so you may have a little boo-boo in there somewhere. Also, there
have been some minor changes in Session behavior since ASP.NET 2.0 came out.
I'd just set a breakpoint on the pertinent line in debug mode and test it
from another machine (not from localhost).
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


SAL said:
Hello,
I'm trying to implement Peter Bromberg's Session Timeout Control:
http://www.eggheadcafe.com/articles/20051228.asp
in ASP.NET 2.0 and am finding that the code never fires. I'm not sure what
I'm doing wrong here. Yes, it would be easy enough to place some of that
code in a class, or test for a particular Session variable but it would be
nice to have a control to drop on a web form and have it just work. Can
anyone tell me what I might be missing. Sorry, I'm still pretty new to
ASP.NET:

Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI

<DefaultProperty("Text"), _
ToolboxData("<{0}:SessionTimeoutControl
runat=server></{0}:SessionTimeoutControl>")> _
Partial Class UserControls_SessionTimeOutControl
Inherits System.Web.UI.UserControl

' reference for this control:
' http://www.eggheadcafe.com/articles/20051228.asp

#Region "Private member variables"
Private mRedirectUrl As String
#End Region

#Region "Public Properties"
<Bindable(True), Category("Appearance"), DefaultValue("")> _
Public Property RedirectUrl() As String
Get
Return mRedirectUrl
End Get
Set(ByVal value As String)
mRedirectUrl = value
End Set
End Property

Public Overrides Property Visible() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'
End Set
End Property

Public Overrides Property EnableViewState() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'MyBase.EnableViewState = value
End Set
End Property
#End Region

#Region "Protected Members"
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
If HttpContext.Current Is Nothing Then
writer.Write("[ *** SessionTimeout: " + Me.ID + " *** ]")
End If
MyBase.Render(writer)
End Sub

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
If Me.mRedirectUrl Is Nothing Then
Throw New InvalidOperationException("RedirectUrl Property Not
Set.")
End If
If Context.Session IsNot Nothing Then
Dim sCookieHeader As String = Page.Request.Headers("Cookie")
If (sCookieHeader IsNot Nothing) And
(sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) Then
If Page.Request.IsAuthenticated Then
FormsAuthentication.SignOut()
End If
Page.Response.Redirect(Me.mRedirectUrl)
End If
End If
End Sub
#End Region
End Class


SAL
 
S

SAL

The code works fine if I stick it in the PageError event.
The real problem is getting the OnPreRender to fire. It doesn't step into
that code at all and I have
AutoEventWireup="true"



SAL

Peter Bromberg said:
SAL,
It worked fine when I wrote the article. Of course, you have translated in
to VB.NET so you may have a little boo-boo in there somewhere. Also, there
have been some minor changes in Session behavior since ASP.NET 2.0 came
out.
I'd just set a breakpoint on the pertinent line in debug mode and test it
from another machine (not from localhost).
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


SAL said:
Hello,
I'm trying to implement Peter Bromberg's Session Timeout Control:
http://www.eggheadcafe.com/articles/20051228.asp
in ASP.NET 2.0 and am finding that the code never fires. I'm not sure
what
I'm doing wrong here. Yes, it would be easy enough to place some of that
code in a class, or test for a particular Session variable but it would
be
nice to have a control to drop on a web form and have it just work. Can
anyone tell me what I might be missing. Sorry, I'm still pretty new to
ASP.NET:

Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI

<DefaultProperty("Text"), _
ToolboxData("<{0}:SessionTimeoutControl
runat=server></{0}:SessionTimeoutControl>")> _
Partial Class UserControls_SessionTimeOutControl
Inherits System.Web.UI.UserControl

' reference for this control:
' http://www.eggheadcafe.com/articles/20051228.asp

#Region "Private member variables"
Private mRedirectUrl As String
#End Region

#Region "Public Properties"
<Bindable(True), Category("Appearance"), DefaultValue("")> _
Public Property RedirectUrl() As String
Get
Return mRedirectUrl
End Get
Set(ByVal value As String)
mRedirectUrl = value
End Set
End Property

Public Overrides Property Visible() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'
End Set
End Property

Public Overrides Property EnableViewState() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'MyBase.EnableViewState = value
End Set
End Property
#End Region

#Region "Protected Members"
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
If HttpContext.Current Is Nothing Then
writer.Write("[ *** SessionTimeout: " + Me.ID + " *** ]")
End If
MyBase.Render(writer)
End Sub

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
If Me.mRedirectUrl Is Nothing Then
Throw New InvalidOperationException("RedirectUrl Property Not
Set.")
End If
If Context.Session IsNot Nothing Then
Dim sCookieHeader As String = Page.Request.Headers("Cookie")
If (sCookieHeader IsNot Nothing) And
(sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) Then
If Page.Request.IsAuthenticated Then
FormsAuthentication.SignOut()
End If
Page.Response.Redirect(Me.mRedirectUrl)
End If
End If
End Sub
#End Region
End Class


SAL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top