473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Base Page class - Request.Cookies

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.To String)
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
Nov 19 '05 #1
2 2548
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.To String)
Then
Return False
Else
Return True
End If
Else
Return False
End If
i hope this works

"Shawn Berg" wrote:
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.To String)
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

Nov 19 '05 #2
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

"Hussein Zahran" <Hu***********@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
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.To String)
Then
Return False
Else
Return True
End If
Else
Return False
End If
i hope this works

"Shawn Berg" wrote:
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.To String)
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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: | last post by:
Please help. After a number of wrong turns and experiments I need advice on login management system to secure our web pages without inconveniencing our visitors or our internal staff. What I...
1
by: Bhupesh Saini | last post by:
I am trying to call a ASPX page using HttpWebRequest class and pass cookie information to it. My ASPX pages gets called just fine, however none of the request cookies are available to the ASPX page....
1
by: Tom Jones | last post by:
Hi, I am using the HttpWebRequest and HttpWebResponse classes to pull information from a web server on the internet. I have an account on one of the webservers that I need to log into...
1
by: Luca Bertoldi | last post by:
Hi all, I developed a base class that inherits from System.Web.UI.Page and I'd like to check some cookies in the constructor of the class. I tried with if (Context.Request.Cookies == null) {...
4
by: Terry Mulvany | last post by:
I have a 'BasePage' (BasePage.cs) derived from System.Web.UI.Page that all my pages inherit from. I need to set some properties (either in the OnInit or constructor) based on a potential...
2
by: Terry Mulvany | last post by:
namespace CIBWeb { public class BasePage : System.Web.UI.Page { public BasePage() { } protected override void OnInit(EventArgs e) {
8
by: Radx | last post by:
Here in my web application, I have a data entry page with serval controls. Some of the controls have autopostback is set true. But the problem is when two or more people are entering data at the...
3
by: Shashank | last post by:
Hi all, I am making a http request to a html page residing on a server through a desktop application . The page is actually an html page with some perl queries on the top. For this page I have...
6
by: jake | last post by:
I think I may why this is happening but I am a little fuzzy as to what the solution should be as I am somewhat new to all of this. My simplified code that generates the error is: public static...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.