ASP.Net cookie -> ASP -> ASP.Net

Discussion in 'ASP .Net' started by Ben, May 27, 2004.

  1. Ben

    Ben Guest

    I'm having problems with cookies from asp.net to asp back to asp.net.

    It seems like I can set a cookie in asp.net fine, and alter it at will, as
    soon as asp touches it, asp.net won't have anything to do with it. Can
    someone please help!

    The code below, going from aspx to aspx, works great the cookie as expected
    goes from qwerty to zxcvb and back. As soon as you hit the asp page, the
    cookie goes to asdfg and stays there no matter how many times you hit it
    from the aspx pages afterwards.

    Please help! I really want to transition slowly to .net, this will hamper
    my ability to do that...

    Thanks,
    Ben

    [fooo.aspx.cs]

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (Request.Cookies["Test"] != null &&
    Request.Cookies["Test"]["Testing"] != null)
    {
    Response.Write(Request.Cookies["Test"]["Testing"]);
    }
    Response.Cookies["Test"]["Testing"] = "qwerty";
    Response.Write("<br><a href=\"fooo2.aspx\">zxcvb</a>");
    Response.Write("<br><a href=\"foo2.asp\">asdfg</a>");
    }

    [fooo.aspx.cs]

    [fooo2.aspx.cs]
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (Request.Cookies["Test"] != null &&
    Request.Cookies["Test"]["Testing"] != null)
    {
    Response.Write(Request.Cookies["Test"]["Testing"]);
    }
    Response.Cookies["Test"]["Testing"] = "zxcvb";
    Response.Write("<br><a href=\"fooo.aspx\">qwerty</a>");
    }
    [fooo2.aspx.cs]

    [foo2.asp]
    <%
    Response.Write(Request.Cookies("Test")("Testing"))
    Response.Cookies("Test")("Testing") = "asdfg"
    Response.Write("<br><a href=""fooo.aspx"">qwerty</a>")
    Response.End()
    %>
    [foo2.asp]
    Ben, May 27, 2004
    #1
    1. Advertising

  2. Hi Ben,

    From your description, you met the problem on sharing cookie value between
    classic asp page and asp.net page, yes?

    As for this problem, I think it's a normal behavior because the ASP and
    ASP.NET have differernt hehavior on dealing with the cookie's path. The
    ASP.NET will by default set the Cookie's Path as "/", its the site's root
    path while the ASP will set it as the "/appname" , the Application's
    path(virtual dir). That's why when after we editing a cookie via ASP page,
    we can never deal with it through ASP.NET page again( the path has been
    corrupted and the ASP.NET can't correctly handled it).

    To resolve it, we can use either of the following means:
    1. If you want to follow the ASP's rule, than manually set the cookie's
    path as "/appname", such as
    Response.Cookies["newfoo"].Path = "/MyWebApp";
    Response.Cookies["newfoo"].Value = "fooovalue";

    2. Or set all the cookie's path as "/" in asp code, just like:
    Response.Cookies("newfoo").Path = "/"
    Response.Cookies("newfoo") = "aspValue"

    I prefer the #2 one, and I 've done a test via the following pages:

    ===================================

    ##fooo.aspx
    if (Request.Cookies["newfoo"] != null)
    {
    Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    }
    Response.Cookies["newfoo"].Path = "/";
    Response.Cookies["newfoo"].Value = "fooovalue";

    Response.Write("<br><a href=\"fooo2.aspx\">fooo2.aspx</a>");
    Response.Write("<br><a href=\"foo2.asp\">foo2.asp</a>");


    ##fooo2.aspx
    if (Request.Cookies["foo"] != null )
    {
    Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    }
    Response.Cookies["newfoo"].Path = "/";
    Response.Cookies["newfoo"].Value = "fooo2value";
    Response.Write("<br><a href=\"fooo.aspx\">fooo.aspx</a>");



    ##foo2.asp
    <%
    for each cookie in Request.Cookies
    Response.Write( cookie & "=" & Request.Cookies(cookie) & "Path:" &
    "<br>")
    next
    Response.Cookies("newfoo").Path = "/"
    Response.Cookies("newfoo") = "aspValue"
    Response.Write("<br><a href=""fooo.aspx"">fooo.aspx</a>")
    Response.End()
    %>

    ===============================================

    That works ok. And here is a web link which has demonstrated the things I
    mentioned above. Thanks.
    #Cookie Behavior in Classic ASP-->ASP.NET
    http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=639


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], May 28, 2004
    #2
    1. Advertising

  3. Ben

    Ben Guest

    Thanks for the fast response. I see what you're talking about, but I'm not
    sure it applies to my problem.

    In my example code from my original post, I can read the cookie in asp.net
    and modify it at will. Asp can also read the cookie. As soon as asp
    modifies the cookie, asp.net can read but not modify the cookie. I can see
    the cookie in asp.net after asp changed it. The value is there as expected,
    but asp.net can no longer modify the cookie.

    Thanks,
    Ben

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Ben,
    >
    > From your description, you met the problem on sharing cookie value between
    > classic asp page and asp.net page, yes?
    >
    > As for this problem, I think it's a normal behavior because the ASP and
    > ASP.NET have differernt hehavior on dealing with the cookie's path. The
    > ASP.NET will by default set the Cookie's Path as "/", its the site's root
    > path while the ASP will set it as the "/appname" , the Application's
    > path(virtual dir). That's why when after we editing a cookie via ASP page,
    > we can never deal with it through ASP.NET page again( the path has been
    > corrupted and the ASP.NET can't correctly handled it).
    >
    > To resolve it, we can use either of the following means:
    > 1. If you want to follow the ASP's rule, than manually set the cookie's
    > path as "/appname", such as
    > Response.Cookies["newfoo"].Path = "/MyWebApp";
    > Response.Cookies["newfoo"].Value = "fooovalue";
    >
    > 2. Or set all the cookie's path as "/" in asp code, just like:
    > Response.Cookies("newfoo").Path = "/"
    > Response.Cookies("newfoo") = "aspValue"
    >
    > I prefer the #2 one, and I 've done a test via the following pages:
    >
    > ===================================
    >
    > ##fooo.aspx
    > if (Request.Cookies["newfoo"] != null)
    > {
    > Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    > Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    > Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    > }
    > Response.Cookies["newfoo"].Path = "/";
    > Response.Cookies["newfoo"].Value = "fooovalue";
    >
    > Response.Write("<br><a href=\"fooo2.aspx\">fooo2.aspx</a>");
    > Response.Write("<br><a href=\"foo2.asp\">foo2.asp</a>");
    >
    >
    > ##fooo2.aspx
    > if (Request.Cookies["foo"] != null )
    > {
    > Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    > Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    > Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    > }
    > Response.Cookies["newfoo"].Path = "/";
    > Response.Cookies["newfoo"].Value = "fooo2value";
    > Response.Write("<br><a href=\"fooo.aspx\">fooo.aspx</a>");
    >
    >
    >
    > ##foo2.asp
    > <%
    > for each cookie in Request.Cookies
    > Response.Write( cookie & "=" & Request.Cookies(cookie) & "Path:" &
    > "<br>")
    > next
    > Response.Cookies("newfoo").Path = "/"
    > Response.Cookies("newfoo") = "aspValue"
    > Response.Write("<br><a href=""fooo.aspx"">fooo.aspx</a>")
    > Response.End()
    > %>
    >
    > ===============================================
    >
    > That works ok. And here is a web link which has demonstrated the things I
    > mentioned above. Thanks.
    > #Cookie Behavior in Classic ASP-->ASP.NET
    > http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=639
    >
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    >
    Ben, May 28, 2004
    #3
  4. Ben

    Ben Guest

    Ok, I was wrong :)

    Thanks,
    Ben

    "Ben" <> wrote in message
    news:RIGtc.764$...
    > Thanks for the fast response. I see what you're talking about, but I'm

    not
    > sure it applies to my problem.
    >
    > In my example code from my original post, I can read the cookie in asp.net
    > and modify it at will. Asp can also read the cookie. As soon as asp
    > modifies the cookie, asp.net can read but not modify the cookie. I can

    see
    > the cookie in asp.net after asp changed it. The value is there as

    expected,
    > but asp.net can no longer modify the cookie.
    >
    > Thanks,
    > Ben
    >
    > "Steven Cheng[MSFT]" <> wrote in message
    > news:...
    > > Hi Ben,
    > >
    > > From your description, you met the problem on sharing cookie value

    between
    > > classic asp page and asp.net page, yes?
    > >
    > > As for this problem, I think it's a normal behavior because the ASP and
    > > ASP.NET have differernt hehavior on dealing with the cookie's path. The
    > > ASP.NET will by default set the Cookie's Path as "/", its the site's

    root
    > > path while the ASP will set it as the "/appname" , the Application's
    > > path(virtual dir). That's why when after we editing a cookie via ASP

    page,
    > > we can never deal with it through ASP.NET page again( the path has been
    > > corrupted and the ASP.NET can't correctly handled it).
    > >
    > > To resolve it, we can use either of the following means:
    > > 1. If you want to follow the ASP's rule, than manually set the cookie's
    > > path as "/appname", such as
    > > Response.Cookies["newfoo"].Path = "/MyWebApp";
    > > Response.Cookies["newfoo"].Value = "fooovalue";
    > >
    > > 2. Or set all the cookie's path as "/" in asp code, just like:
    > > Response.Cookies("newfoo").Path = "/"
    > > Response.Cookies("newfoo") = "aspValue"
    > >
    > > I prefer the #2 one, and I 've done a test via the following pages:
    > >
    > > ===================================
    > >
    > > ##fooo.aspx
    > > if (Request.Cookies["newfoo"] != null)
    > > {
    > > Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    > > Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    > > Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    > > }
    > > Response.Cookies["newfoo"].Path = "/";
    > > Response.Cookies["newfoo"].Value = "fooovalue";
    > >
    > > Response.Write("<br><a href=\"fooo2.aspx\">fooo2.aspx</a>");
    > > Response.Write("<br><a href=\"foo2.asp\">foo2.asp</a>");
    > >
    > >
    > > ##fooo2.aspx
    > > if (Request.Cookies["foo"] != null )
    > > {
    > > Response.Write("<br>" + Request.Cookies["newfoo"].Domain);
    > > Response.Write("<br>"+ Request.Cookies["newfoo"].Path );
    > > Response.Write("<br>" + Request.Cookies["newfoo"].Value);
    > > }
    > > Response.Cookies["newfoo"].Path = "/";
    > > Response.Cookies["newfoo"].Value = "fooo2value";
    > > Response.Write("<br><a href=\"fooo.aspx\">fooo.aspx</a>");
    > >
    > >
    > >
    > > ##foo2.asp
    > > <%
    > > for each cookie in Request.Cookies
    > > Response.Write( cookie & "=" & Request.Cookies(cookie) & "Path:" &
    > > "<br>")
    > > next
    > > Response.Cookies("newfoo").Path = "/"
    > > Response.Cookies("newfoo") = "aspValue"
    > > Response.Write("<br><a href=""fooo.aspx"">fooo.aspx</a>")
    > > Response.End()
    > > %>
    > >
    > > ===============================================
    > >
    > > That works ok. And here is a web link which has demonstrated the things

    I
    > > mentioned above. Thanks.
    > > #Cookie Behavior in Classic ASP-->ASP.NET
    > > http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=639
    > >
    > >
    > > Regards,
    > >
    > > Steven Cheng
    > > Microsoft Online Support
    > >
    > > Get Secure! www.microsoft.com/security
    > > (This posting is provided "AS IS", with no warranties, and confers no
    > > rights.)
    > >
    > > Get Preview at ASP.NET whidbey
    > > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    > >
    > >

    >
    >
    >
    Ben, May 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    5,831
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    562
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,471
    Bruce Barker
    Jan 10, 2006
  4. =?Utf-8?B?TnVubw==?=

    Convert a PHP cookie to an ASP.NET cookie

    =?Utf-8?B?TnVubw==?=, Jan 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    437
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 31, 2006
  5. william

    System.Net.Cookie vs System.Web.Cookie

    william, Apr 11, 2008, in forum: ASP .Net Security
    Replies:
    4
    Views:
    1,246
    Dominick Baier
    Apr 14, 2008
Loading...

Share This Page