Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 2nd, 2006, 06:07 PM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Handling Cookies

Hi All

Probably a 'School boy' error but for some reason when I check to see if a cookie value exists I get the following error if the cookie doesn't:

Code:
System.NullReferenceException: Object reference not set to an
instance of an object
How would be best to handle this?

The code I was using is:

Code:
If Request.Cookies("Cookie1")("cookie12") = "" Then
      Response.Redirect("login.aspx?Stat=FAIL")
End If
What I am trying to do is create a simple form of authentication against a user database table which creates a cookie if their credentials match. If the cookie doesn't exist then they are redirected to login page.

Many thanks

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
Reply With Quote
  #2 (permalink)  
Old February 3rd, 2006, 07:06 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Since you're using a multi valued cookie (with Keys) I think you need to check the entire cookie first before you check one of its keys:

If Request.Cookies("Cookie1") IsNot Nothing Then
  If Request.Cookies("Cookie1")("cookie12") = "" Then
        Response.Redirect("login.aspx?Stat=FAIL")
  End If
End If

You may need to repeat the same check for the Cookie12 check....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old March 22nd, 2006, 11:14 AM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Hi Imar

Just pulling this one from the past...

What would be the best way to check to see if a cookie exists to share on both an ASP(vbscript) and ASP.NET (VB.Net) page... the code provided above I can't seem to get it to work on an ASP page.

Many thanks

Rit
Reply With Quote
  #4 (permalink)  
Old March 22nd, 2006, 02:59 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Rit,

In ASP you can check if a cookie has a value by comparing against "". So, this should work:
Code:
If Request.Cookies("Cookie1")  & "" = "" Then
  Response.Write("Nothing<br />")
Else
  Response.Write("NOT Nothing<br />")
End If
Once you know you have a valid cookie, you can use its HasKeys property to see if it has any, eeuuh, keys. ;)

See here for more information: http://www.w3schools.com/asp/asp_cookies.asp

Hth,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Vamos by Pixies (Track 12 from the album: Surfer Rosa & Come On Pilgrim) What's This?
Reply With Quote
  #5 (permalink)  
Old March 22nd, 2006, 03:47 PM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Sorry Imar, Still can't get it to go buddy.

Here is what I have...

I have an include file that displays a different menu depending on whether you are logged in (cookie created) or logged out (no cookie). The code looks similar to the below:

Code:
If request.Cookies("recuser")("islogin") = 1 Then
  menu1
Else
  menu2
End If
.. now this works fine in my asp3 pages but the same include is also referenced within my asp.net2 pages too but this code causes the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

When I test to see if the cookie exists first using the following code, this work fine in asp.net2 pages but not in asp3:

Code:
If Request.Cookies("recuser") IsNot Nothing Then

  If request.Cookies("recuser")("islogin") = 1 Then
    menu1
  Else
    menu2
  End If

Ensd If

Your last example seems to work fine in asp3 but not in asp.net2.

I suppose what I am trying to find is a mutual piece of code that will work in both versions of ASP... is this possible?

Warm Regards

Rit
Reply With Quote
  #6 (permalink)  
Old March 22nd, 2006, 03:56 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yeah, the last code I posted was for ASP 3, not .NET

Technically, you can use ASP includes in an ASP.NET page, but for not more than simple UI stuff (that is, HTML).

However, I don't recommend to do that. There are much better alternatives to ASP includes in .NET (Master Pages, User Controls and so on), so you should stay away from old skool includes.

Also, you cannot share code like this. .NET code is (partially) compiled at design time while ASP code is interpreted at run-time. Besides this, there are many other reason why this wouldn't work (the syntax problem you run into is one of them).

So, bottom line: duplicate the code for both platforms. Alternatively, you could move some of your shared code to a VB DLL or a .NET assembly and use one of them in both platforms. Not an easy undertaking, could result in odd behavior (components having references to things like Response and so on) but it'll work.

My recommendation: duplicate the code in both platforms...

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #7 (permalink)  
Old March 22nd, 2006, 04:21 PM
Friend of Wrox
 
Join Date: Oct 2005
Location: , , United Kingdom.
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

thanks Imar, Duplication is the way on this occassion... again many thanks for your knowledge.

Rit
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Server acting as browser - handling cookies? Philibuster PHP How-To 1 August 15th, 2006 09:39 PM
Cookies hastikeyvan Classic ASP Professional 5 May 29th, 2006 07:36 AM
Cookies shabboleth Beginning PHP 3 December 30th, 2003 10:32 AM
Help!About Cookies jelsen ASP.NET 1.0 and 1.1 Basics 2 September 9th, 2003 08:36 AM



All times are GMT -4. The time now is 02:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.