Cookie files are being named differently

Posted on 2006-05-12
Last Modified: 2008-03-10
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:/
Question by:tsb5270
    LVL 33

    Expert Comment

    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.
    LVL 33

    Expert Comment

    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.

    Author Comment

    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" : "" );
    LVL 33

    Expert Comment

    This question should be PAQ'd, not deleted, the author solved his/her problem.

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now