Base Page class - Request.Cookies

S

Shawn Berg

Some of my pages in the app I am developing inherit from a BasePage class I
have created. I have done plenty of these in the past and they work fine.
Now, however, I have an additional requirement.

In my BasePage class I need to check the user's browser for a particular
cookie, and if certain conditions aren't met, redirect the user to another
page. You can see the code for my BasePage class below. The problem lies in
the LoggedIn function. I am getting the error
"System.NullReferenceException: Object reference not set to an instance of
an object" on the first line of that function. Any ideas?

Public Class SecureAdminBasePage
Inherits System.Web.UI.Page

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
Me.Controls.AddAt(0,
LoadControl("/UserControls/SecureAdminHeader.ascx"))
MyBase.OnInit(e)
Me.Controls.Add(LoadControl("/UserControls/SecureAdminFooter.ascx"))
End Sub

Private Function LoggedIn()
If Not MyBase.Request.Cookies("TSRAdmin").Value.ToString = "TRUE" _
Or Not IsNumeric(MyBase.Request.Cookies("au_id").Value.ToString)
Then
Return False
Else
Return True
End If
End Function

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)

'Be sure user is logged in
If Not LoggedIn() Then
Response.Redirect("/admin/login/")
End If
End Sub

End Class
 
G

Guest

Hi Shawn
All you will need is to check if the cookie already exists or not like this
If Not MyBase.Request.Cookies("TSRAdmin") is Nothing then
If Not MyBase.Request.Cookies("TSRAdmin").Value.ToString = "TRUE" _
Or Not IsNumeric(MyBase.Request.Cookies("au_id").Value.ToString)
Then
Return False
Else
Return True
End If
Else
Return False
End If
i hope this works
 
S

Shawn Berg

Hussein,

Your suggestion has definitely help me resolve this problem. Thanks.

I am still experiencing one oddity, however. The cookies I am trying to read
in ASP.NET are being set by classic ASP pages. In order to read the cookie
in ASP.NET I have to "URLEncode" the cookie's name manually, like so:

Request.Cookies("au%5Fid")

When I use the following ASP.NET cannot find the cookie:

Request.Cookies("au_id")

Is this a known problem with reading cookies written by classic ASP pages?

Shawn
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top