Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if a cookie value exists and then redirecting

Posted on 2010-11-17
2
Medium Priority
?
243 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

618 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