Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Help with cookies

I'm trying to use cookies on my intranet site so that when users visit a certain password protected page, the page will look for a cookie to see if they have already logged in or not, and if they have it will forward them past the login page so they don't have to re-enter their username or password.  I believe the adding part is working (I can get a cookie onto the user's machine after they have successfully logged in once.  But my problem is reading this cookie from the users machine.  It seems that the cookie is only valid while the user's browser is open.  Once the browser is closed and the user tries to access that page again, I get a Page Cannot be Displayed error.  Here is the code:

To add the cookie after a user has logged in:
Response.Cookies("auth") = "yes"
Response.Cookies("auth").Expires = Date + 365

Then to read the cookie if they visit this login.asp page again:
MM_redirectLoginSuccess="Calendars/NewEvent.asp?ID="&Recordset1__MMColParam
MM_redirectLoginFailed="AccessDenied.htm"

If (Request.Cookies("auth") = "yes") Then
      Response.redirect(MM_redirectLoginSuccess)
End If

the MM_redirectLoginSuccess variable is the page that is displayed after you successfuly login and I want to automatically send the user there if they have this cookie (auth) with a value of yes.  

If you need to see more code just ask!

Thanks
0
sre23
Asked:
sre23
  • 7
  • 6
1 Solution
 
Saqib KhanSenior DeveloperCommented:
You are missing the Key Value


try

Response.Cookies("MYCOOKIE")("auth") = "yes"

so add ("MYCOOKIE") to all of your Cookie Call's.

Cookie requires two parts.
1. Cookie name
2. Cookie Key

You must create the cookie before the HTTP headers are written to the client browser. Basically that means you need to create cookies before the <HTML> tag on your page.
0
 
sre23Author Commented:
so when I add the cookie the code should be:

Response.Cookies("MYCOOKIE")("auth") = "yes"
Response.Cookies("MYCOOKIE")("auth").Expires = Date + 365

and then when I try to read the cookie it should be:

If (Request.Cookies("MYCOOKIE")("auth") = "yes") Then
    Response.redirect(MM_redirectLoginSuccess)
End If

Is this correct?
0
 
sre23Author Commented:
that can't be right because when I try that I get a page cannot be displayed error when trying to add the cookie to the user's machine.  What am I missing?
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
Saqib KhanSenior DeveloperCommented:
Please Post the Whole error message
0
 
sre23Author Commented:
Microsoft VBScript runtime error '800a01b6'

Object doesn't support this property or method: 'Response.Cookies(...)'

/MG_Info/Login.asp, line 56

Line 56 is Response.Cookies("MGC")("auth").Expires = Date + 365
0
 
sre23Author Commented:
Here's the more generic error message:

The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed.

--------------------------------------------------------------------------------

Please try the following:

Open the mgc home page, and then look for links to the information you want.
Click the  Refresh button, or try again later.

Click  Search to look for information on the Internet.
You can also see a list of related sites.




HTTP 500 - Internal server error
Internet Explorer  
0
 
Saqib KhanSenior DeveloperCommented:
I am sorry, Syntax i mentioned above was for dictionary cookies, you dont really need it.

Code you posted is correct, I dont see any problem with it.

Your Cookies are disabled in browser?
and you generating cookies on the TOP of your page?
0
 
sre23Author Commented:
yes the code is at the top of the page (before <html>) and I believe cookies are enabled because it works fine until I close the browser.  Once I close the browser and re-open it, it does not work.  I'm not sure if the part that fails is when the browser is trying to access the cookie, or if it fails when it is trying to redirect the user past the current page.

If you notice my redirect page uses a variable from the url and that code comes right before the request for the cookie.  Here is the code starting from the very top to the point that it tries to access the cookie and redirect the user past the login page (the current page).

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/conIntranet.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "0"
If (Request.QueryString("ID") <> "") Then
  Recordset1__MMColParam = Request.QueryString("ID")
End If

MM_redirectLoginSuccess="Calendars/NewEvent.asp?ID="&Recordset1__MMColParam
MM_redirectLoginFailed="AccessDenied.htm"

If (Request.Cookies("auth") = "yes") Then      
      Response.redirect(MM_redirectLoginSuccess)
End If
0
 
Saqib KhanSenior DeveloperCommented:
Print the Value on the Top of your Page

<%=Request.Cookies("auth")%>
<% Response.end()%>

What does it show?
0
 
sre23Author Commented:
ok, I found the problem... the redirect page has security enabled (Restrict Acces To Page behavior) so when I try to forward there automatically I am actually getting sent to the access denied page.  This becomes a whole different problem now.  Is there a way around that?  I've seen sites (such as this one) that have a "Remember Me" feature so that users don't need to log in each time.  I figured the easiest way to do that was with a cookie, but how do you do it?
0
 
Saqib KhanSenior DeveloperCommented:
The way you doing is the way to store user name ans password on user Machine.
it is Done with Cookies.

That page has the restrict access, modify that Page to overwrite the Login Process if Cookie is set to True.
0
 
sre23Author Commented:
thanks so much for your help... sorry to make you take the longest route to fix that one!
0
 
Saqib KhanSenior DeveloperCommented:
No problem:)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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