Solved

Check if a cookie value exists and then redirecting

Posted on 2010-11-17
2
239 Views
Last Modified: 2012-05-10
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
            Response.Redirect("Default.aspx")
        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?
0
Comment
Question by:cdemott33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34158352
You need to first test whether the cookie exists at all

If Request.Cookies("UserCookie") is nothing then
.. redirect
ElseIf Request.Cookies("UserCookie")("UserID")....
0
 

Author Closing Comment

by:cdemott33
ID: 34158518
Perfect... Thank you.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question