[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:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 78: Response.Cookies("User_ID").Value = User_ID
Line 79:
[HL="#FFFF00"]Line 80: Response.Redirect(Convert.ToString(IIf(IsNothing(Request.Cookies("LoginTarget")), _
RedirectPage.ToString, Request.Cookies("LoginTarget").Value)), False)[/HL]
Line 81: 'Response.Redirect(RedirectPage.ToString, False)
Line 82: Else
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
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?
Re: Check if Cookie has been set?
I'd do:
VB Code:
Dim TargetCookie As Cookie = Request.Cookies.Item("LoginTarget")
If TargetCookie Is Nothing Then
Response.Redirect RedirectPage
Else
Response.Redirect TargetCookie.Value
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:
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:
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
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
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
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