Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2006, 06:07 PM
Friend of Wrox
 
Join Date: Oct 2005
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
 
Old February 3rd, 2006, 07:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
 
Old March 22nd, 2006, 11:14 AM
Friend of Wrox
 
Join Date: Oct 2005
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
 
Old March 22nd, 2006, 02:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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?
 
Old March 22nd, 2006, 03:47 PM
Friend of Wrox
 
Join Date: Oct 2005
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
 
Old March 22nd, 2006, 03:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
 
Old March 22nd, 2006, 04:21 PM
Friend of Wrox
 
Join Date: Oct 2005
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.