• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Check if a cookie value exists and then redirecting

Hi - I need a bit of help with cookies.  I created a cookie called UserCookie that hold two values; a UserID and a Name.  

I want to check if one of the two values exists before a specific page loads.  If it doesn't exist I want to redirect them to the login page.  I wrote something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Cookies("UserCookie")("UserID") Is Nothing Then
        End If
End Sub

However, when my page loads I get the following error:

Object reference not set to an instance of an object.

How can I fix this problem?
1 Solution
You need to first test whether the cookie exists at all

If Request.Cookies("UserCookie") is nothing then
.. redirect
ElseIf Request.Cookies("UserCookie")("UserID")....
cdemott33Author Commented:
Perfect... Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now