p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Handling Cookies (http://p2p.wrox.com/showthread.php?t=37956)

rit01 February 2nd, 2006 06:07 PM

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

Imar February 3rd, 2006 07:06 PM

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.

rit01 March 22nd, 2006 11:14 AM

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

Imar March 22nd, 2006 02:59 PM

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?

rit01 March 22nd, 2006 03:47 PM

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

Imar March 22nd, 2006 03:56 PM

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.

rit01 March 22nd, 2006 04:21 PM

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

Rit


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

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