473,657 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.NullRef erenceException : Object reference not set to an instance of
an object" on the first line of that function. Any ideas?

Public Class SecureAdminBase Page
Inherits System.Web.UI.P age

Protected Overrides Sub OnInit(ByVal e As System.EventArg s)
Me.Controls.Add At(0,
LoadControl("/UserControls/SecureAdminHead er.ascx"))
MyBase.OnInit(e )
Me.Controls.Add (LoadControl("/UserControls/SecureAdminFoot er.ascx"))
End Sub

Private Function LoggedIn()
If Not MyBase.Request. Cookies("TSRAdm in").Value.ToSt ring = "TRUE" _
Or Not IsNumeric(MyBas e.Request.Cooki es("au_id").Val ue.ToString)
Then
Return False
Else
Return True
End If
End Function

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

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

End Class
Nov 19 '05 #1
2 2558
Hi Shawn
All you will need is to check if the cookie already exists or not like this
If Not MyBase.Request. Cookies("TSRAdm in") is Nothing then
If Not MyBase.Request. Cookies("TSRAdm in").Value.ToSt ring = "TRUE" _
Or Not IsNumeric(MyBas e.Request.Cooki es("au_id").Val ue.ToString)
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.NullRef erenceException : Object reference not set to an instance of
an object" on the first line of that function. Any ideas?

Public Class SecureAdminBase Page
Inherits System.Web.UI.P age

Protected Overrides Sub OnInit(ByVal e As System.EventArg s)
Me.Controls.Add At(0,
LoadControl("/UserControls/SecureAdminHead er.ascx"))
MyBase.OnInit(e )
Me.Controls.Add (LoadControl("/UserControls/SecureAdminFoot er.ascx"))
End Sub

Private Function LoggedIn()
If Not MyBase.Request. Cookies("TSRAdm in").Value.ToSt ring = "TRUE" _
Or Not IsNumeric(MyBas e.Request.Cooki es("au_id").Val ue.ToString)
Then
Return False
Else
Return True
End If
End Function

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

'Be sure user is logged in
If Not LoggedIn() Then
Response.Redire ct("/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.mic rosoft.com> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
Hi Shawn
All you will need is to check if the cookie already exists or not like
this
If Not MyBase.Request. Cookies("TSRAdm in") is Nothing then
If Not MyBase.Request. Cookies("TSRAdm in").Value.ToSt ring = "TRUE" _
Or Not IsNumeric(MyBas e.Request.Cooki es("au_id").Val ue.ToString)
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.NullRef erenceException : Object reference not set to an instance
of
an object" on the first line of that function. Any ideas?

Public Class SecureAdminBase Page
Inherits System.Web.UI.P age

Protected Overrides Sub OnInit(ByVal e As System.EventArg s)
Me.Controls.Add At(0,
LoadControl("/UserControls/SecureAdminHead er.ascx"))
MyBase.OnInit(e )
Me.Controls.Add (LoadControl("/UserControls/SecureAdminFoot er.ascx"))
End Sub

Private Function LoggedIn()
If Not MyBase.Request. Cookies("TSRAdm in").Value.ToSt ring = "TRUE" _
Or Not IsNumeric(MyBas e.Request.Cooki es("au_id").Val ue.ToString)
Then
Return False
Else
Return True
End If
End Function

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

'Be sure user is logged in
If Not LoggedIn() Then
Response.Redire ct("/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
2459
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 need: A system whereby the user only has to register ONCE and he will have automatic entry to ANY page without havinto to RE-LOGIN even if he comes in
1
1847
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. What do I need to do to have the cookies sent along with the request Below is the code snippet from the function I an using I am using to achieve the above. EVerything works except that I do not see any cookies in request object for the ASPX page...
1
7897
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 programatically. The connection is not secure (not https, etc.). The login page just has simple name & password textboxes on it. Can someone please tell me how I can programatically login using the
1
1289
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) { Context.Response.Write("<script language='javascript'>document.location='/" + currContext.Request.ApplicationPath + "/default.asp?msg=Please, log on&goto=" + ModuleCode + "';</script>"); Context.Response.End();
4
5090
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 Request.Cookie or a name/value pair in Request.QueryString. But I am getting a 'System.NullReferenceException: Object reference not set to an instance of an object.' error when I try to view any aspx derived from this BasePage. I am using...
2
1706
by: Terry Mulvany | last post by:
namespace CIBWeb { public class BasePage : System.Web.UI.Page { public BasePage() { } protected override void OnInit(EventArgs e) {
8
2189
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 same time, in the middle of my data entry , it is brining someelse data on my screen. I look everywhere i could not find the solution. Please help me Radx
3
2346
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 some cookies set on my local browser. The content of the page is dependent on the cookie value My problem is that when I make the request through a browser, I am getting the right contents, but when I make request through my desktop application the...
6
1377
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 string getCartCookieValue() { if (Request.Cookies != null) {
0
8315
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.