Results 1 to 7 of 7

Thread: [RESOLVED] Check if Cookie has been set?

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] Check if Cookie has been set?

    Hi,

    I'm getting an Object reference error and I'm sure the problem is because the LoginTarget cookie hasn't been set, this is possible if a user came to the login page first, without attempting to visit a restricted page first.

    I'm using IsNothing to check to see if it's been created but it doesn't seem to be working does anyone know of a better way of handling this?

    VB Code:
    1. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    2.  
    3. Source Error:
    4.  
    5. Line 78:             Response.Cookies("User_ID").Value = User_ID
    6. Line 79:
    7. [HL="#FFFF00"]Line 80:             Response.Redirect(Convert.ToString(IIf(IsNothing(Request.Cookies("LoginTarget")), _
    8.                                   RedirectPage.ToString, Request.Cookies("LoginTarget").Value)), False)[/HL]
    9. Line 81:             'Response.Redirect(RedirectPage.ToString, False)
    10. Line 82:         Else
    Last edited by aconybeare; Oct 2nd, 2005 at 04:16 PM. Reason: retitle and improve detail

  2. #2
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: Check if Cookie has been set?

    in my application i dont have any links to the login page, therefore a user cannot navigate to a login page,
    they navigate to the restricted pages and therfore only go to the login if required to view page
    this maybe an answer that isnt what you want but it maybe an idea,
    more experienced useres may be able to tell u more
    it works 60% of the time, all the time.

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Check if Cookie has been set?

    This code is part of a user control (LoginCheck) which checks if a user is logged in if not then they are redirected to the login page, once logged I'm attempting to send them to the page they were originally trying to get to.

    If I can't get this to work I can just send them to the default page and they then find their own way back to where they were trying to get to.

    I was originally trying to use If IsNull instead of IsNothing but .Net says it's not declared does anyone know which namespace it belongs to?

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Check if Cookie has been set?

    I'd do:
    VB Code:
    1. Dim TargetCookie As Cookie = Request.Cookies.Item("LoginTarget")
    2.  
    3.    If TargetCookie Is Nothing Then
    4.       Response.Redirect RedirectPage
    5.    Else
    6.       Response.Redirect TargetCookie.Value
    7.    End If
    I believe the reason you're getting the error is that both the True and False bits of the IIF statement are calculated regardless of which one is going to be selected. If the cookie is nothing then you're still trying to get it's value.
    VB Code:
    1. If Not (TargetCookie Is Nothing) AndAlso Not (TargetCookie.Value=String.Empty) Then
    Doing the above will ONLY validate the 2nd part of the IF the 1st part is true.
    If you just did:
    VB Code:
    1. If Not (TargetCookie Is Nothing) And Not (TargetCookie.Value=String.Empty) Then
    Then this will error. You must use the AndAlso or OrElse command keywords.
    This is not relevant to your question, but thought it would be usefull info for you.


    Woka

  5. #5
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: Check if Cookie has been set?

    i use this in my pages to identify the user

    Session("company") = Page.User.Identity.Name
    lblUser.Text = Page.User.Identity.Name
    Dim user As String
    user = lblUser.Text

    its a label to say who is logged in at the current time
    this prob isnt ideal but it seems to work for me


    then in your webconfig file
    you need somethin of
    windows authentication forms
    deny users ?

    then any page you do want the user to see without being logged in
    webconfig e.g.

    <location path="WebForm1.aspx">
    <system.web>
    <authorization>
    <allow users="*" />
    </authorization>
    </system.web>
    </location>

    i dont know if this helps
    it works 60% of the time, all the time.

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Check if Cookie has been set?

    Take a peek at my "My ASP.NET Code Examples:" in my sig. There are two demo apps that do exactly what you're looking for. May be done slightly different to what you have done as I have passed the redirect page as a URL param and not a cookie. Think a cookie may be a little over kill for this purpose

    Anyways take a peek, you may find it helpful.

    Woka

  7. #7

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Check if Cookie has been set?

    Woka,

    Thanks a bunch, Think this puts me on my way

    d2005, thanks I'll keep your suggestion in mind although I'm trying not to use session variables

    Cheers Al

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width