Link to home
Start Free TrialLog in
Avatar of tsb5270
tsb5270

asked on

Cookie files are being named differently

This is a question that pertains both to javascript and ASP.NET.   I have a .NET web page that creates and reads cookies.  The page also uses a liberal sprinkling of javascript, which also needs to create and read cookies.  The problem I'm experiencing is that the cookies being made on the .NET side are being named differently than the ones on the javascript side.  That is to say, when I look in the cookies folder I see two files.  One is named myusername@localhost[1].txt and the other is names myusername@Myapplicationname[2].txt.  I'm a bit confused as to why this is happening and what I can do so that I only have one cookie for the application.  The .NET code needs to be able to read the cookie value for cookies created in the javascript and visa versa.  

When the cookies are being created, I am NOT indicating a domain name or a path.
The application is NOT https:/
Avatar of raterus
raterus
Flag of United States of America image

This is probably something you can figure out rather quickly by examining the HTTP headers, and looking what is happening with the cookies.  I'm guessing you are using IE, so try this program to do it.

http://www.blunck.info/iehttpheaders.html
also, if this is an application that is publically available and you can give a URL, I could see what was up in a matter of minutes if I could access it.
Avatar of tsb5270
tsb5270

ASKER

The solution seemed relatively easy.  I need to add the path attribute.

On the .NET side I created the cookie this way:

 Shared Sub SetCookie(ByRef _httpContext As HttpContext, ByVal CookieName As String, ByVal CookieValue As String, ByVal Expires As Integer)
        Dim appCookie As HttpCookie = New HttpCookie(CookieName)
        appCookie.Value = CookieValue
        appCookie.Path = "/"
        appCookie.Expires = DateTime.Now.AddMinutes(Expires)
    End Sub

In javscript:

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" + value  +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
This question should be PAQ'd, not deleted, the author solved his/her problem.
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial