ASP NET 2.0 Cookie

Discussion in 'ASP .Net' started by icanhelp33@gmail.com, May 6, 2008.

  1. Guest

    I have a C# utility that runs in ASP website. When user logs in the
    ASP code writes the cookie using Response.Cookie. I try to read the
    same cookie in C#. I am unable to read it in C# code.

    I am using Request.Cookies["cookiename"].Value.ToString(). I can read
    the same cookie in ASP code.
    If I say Request.Cookies("CookieName") in ASP that works fine..
    , May 6, 2008
    #1
    1. Advertising

  2. re:
    !> I am using Request.Cookies["cookiename"].Value.ToString()

    Try either of these approaches :

    1.

    if(Request.Cookies["cookieName"] != null)
    Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value);

    2.
    if(Request.Cookies["cookieName"] != null)
    {
    HttpCookie aCookie = Request.Cookies["cookieName"];
    Label1.Text = Server.HtmlEncode(aCookie.Value);
    }

    Before trying to get the value of a cookie, you should make sure that the cookie exists;
    if the cookie does not exist, you will get a NullReferenceException exception.

    Notice also that the HtmlEncode method was called to encode the contents of a cookie
    before displaying it in the page. This makes certain that a malicious user has not added
    executable script into the cookie.



    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    <> wrote in message news:...
    >I have a C# utility that runs in ASP website. When user logs in the
    > ASP code writes the cookie using Response.Cookie. I try to read the
    > same cookie in C#. I am unable to read it in C# code.
    >
    > I am using Request.Cookies["cookiename"].Value.ToString(). I can read
    > the same cookie in ASP code.
    > If I say Request.Cookies("CookieName") in ASP that works fine..
    >
    >
    Juan T. Llibre, May 6, 2008
    #2
    1. Advertising

  3. Guest

    I know how to read the cookie. The problem is when I try to read it in
    C# it always returns null. In ASP it returns the value of the cookie.
    , May 6, 2008
    #3
  4. re:
    !> I know how to read the cookie.
    !> The problem is when I try to read it in C# it always returns null.

    Then, either the code you're using doesn't read the cookie properly,
    or the cookie isn't being set correctly.

    Have you tried the code I posted to read cookies ?

    As for setting cookies, here's 2 sample C# code fragments :

    1.
    Response.Cookies["userName"].Value = "patrick";
    Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

    2.
    HttpCookie aCookie = new HttpCookie("lastVisit");
    aCookie.Value = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);

    How are you setting the cookie ?
    Are you expiring the cookie ?



    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    <> wrote in message news:...
    >I know how to read the cookie. The problem is when I try to read it in
    > C# it always returns null. In ASP it returns the value of the cookie.
    >
    Juan T. Llibre, May 6, 2008
    #4
  5. Cookies should be able to read regardless
    of the language/platform used to write them.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    "Patrice" <http://www.chez.com/scribe/> wrote in message news:D...
    > I'm really not sure you can do that... ASP And ASP.NET are separate engines... I'm even surprised you don't get an
    > exception as from the point of view of the C# utility (how do you run this one ? This is exposed as a com object ?)
    > there is IMO no current (ASP.NET) request...
    >
    > You may want to elaborate a bit on your architecture and/or what you are trying to do...
    >
    > --
    > Patrice
    >
    > <> a écrit dans le message de groupe de discussion :
    > ...
    >> I have a C# utility that runs in ASP website. When user logs in the
    >> ASP code writes the cookie using Response.Cookie. I try to read the
    >> same cookie in C#. I am unable to read it in C# code.
    >>
    >> I am using Request.Cookies["cookiename"].Value.ToString(). I can read
    >> the same cookie in ASP code.
    >> If I say Request.Cookies("CookieName") in ASP that works fine..
    >>
    >>

    >
    Juan T. Llibre, May 6, 2008
    #5
  6. Typo alert...

    That should have been :

    Cookies should be able to be read regardless
    of the language/platform used to write them.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    "Juan T. Llibre" <> wrote in message news:%...
    > Cookies should be able to read regardless
    > of the language/platform used to write them.
    >
    >
    >
    >
    > Juan T. Llibre, asp.net MVP
    > asp.net faq : http://asp.net.do/faq/
    > foros de asp.net, en español : http://asp.net.do/foros/
    > ======================================
    > "Patrice" <http://www.chez.com/scribe/> wrote in message news:D...
    >> I'm really not sure you can do that... ASP And ASP.NET are separate engines... I'm even surprised you don't get an
    >> exception as from the point of view of the C# utility (how do you run this one ? This is exposed as a com object ?)
    >> there is IMO no current (ASP.NET) request...
    >>
    >> You may want to elaborate a bit on your architecture and/or what you are trying to do...
    >>
    >> --
    >> Patrice
    >>
    >> <> a écrit dans le message de groupe de discussion :
    >> ...
    >>> I have a C# utility that runs in ASP website. When user logs in the
    >>> ASP code writes the cookie using Response.Cookie. I try to read the
    >>> same cookie in C#. I am unable to read it in C# code.
    >>>
    >>> I am using Request.Cookies["cookiename"].Value.ToString(). I can read
    >>> the same cookie in ASP code.
    >>> If I say Request.Cookies("CookieName") in ASP that works fine..
    >>>
    >>>

    >>

    >
    >
    Juan T. Llibre, May 6, 2008
    #6
  7. Make sure the Path property of your cookie is set. , e.g. "/". There are
    minor inconsistencies in classic ASP cookies and ASP.NET cookies.
    Peter
    <> wrote in message
    news:...
    >I know how to read the cookie. The problem is when I try to read it in
    > C# it always returns null. In ASP it returns the value of the cookie.
    >
    Peter Bromberg [C# MVP], May 8, 2008
    #7
  8. To be more specific:

    Cookie Interop - ASP -->ASPX:

    There is a change in cookie behavior between ASP and ASPX. In brief: If you
    do NOT
    specifically set a cookie path, ASP will automatically set the path to:
    your web application's name --
    "/AppName"
    If you do NOT specifically set a path, ASPX will automatically set the path
    to:
    your server name --
    "/"

    To make ASP behave like ASPX, then add --
    Response.Cookies("...").Path = "/" -- to your ASP code

    To make ASPX behave like ASP, then add --
    MyCookieObject.Path = "/AppName" -- to your ASPX code.

    --Peter
    <> wrote in message
    news:...
    >I know how to read the cookie. The problem is when I try to read it in
    > C# it always returns null. In ASP it returns the value of the cookie.
    >
    Peter Bromberg [C# MVP], May 8, 2008
    #8
  9. eliza Guest

    A brief idea about Cookie ASP.NET

    What is a Cookie ?

    A Cookie is an object is used to store the sort amount of data onto client end. It executes on server and resides on client browser.

    How does Cookie work ?

    when users request a page from your site, your application sends not just a page, but a cookie containing some data. When the user's browser gets the page, the browser also gets the cookie.Later, the user requests a page from your site again.If the cookie exists, the browser sends the cookie to your site along with the page
    request.

    What are different type of Cookie ?

    Session Cookie : This Cookie will expire once the browser is closed.
    Persistent Cookie : This Cookie will exists in browser till the time of expire.
    How much can we store in a Cookie ?

    Cookie specifications suggest that browsers should support a minimal number of cookies . In particular, an internet browser is expected to be able to store at least 300 cookies of four kilobytes each (both name and value count towards this 4 kilobyte limit), and at least 20 cookies per server or domain, so it is not a good idea to use a different cookie for each variable that has to be saved. It's better to save all needed data into one single cookie.



    Juan T. Llibre wrote:

    Re: ASP NET 2.0 Cookie
    06-May-08

    re
    !> I am using Request.Cookies["cookiename"].Value.ToString(

    Try either of these approaches

    1

    if(Request.Cookies["cookieName"] != null
    Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value)

    2
    if(Request.Cookies["cookieName"] != null

    HttpCookie aCookie = Request.Cookies["cookieName"]
    Label1.Text = Server.HtmlEncode(aCookie.Value)


    Before trying to get the value of a cookie, you should make sure that the cookie exists
    if the cookie does not exist, you will get a NullReferenceException exception

    Notice also that the HtmlEncode method was called to encode the contents of a cooki
    before displaying it in the page. This makes certain that a malicious user has not adde
    executable script into the cookie


    Juan T. Llibre, asp.net MV
    asp.net faq : http://asp.net.do/faq
    foros de asp.net, en espa?ol : http://asp.net.do/foros
    =====================================
    <> wrote in message news:...

    Previous Posts In This Thread:

    On Tuesday, May 06, 2008 1:06 PM
    Juan T. Llibre wrote:

    Re: ASP NET 2.0 Cookie
    re
    !> I am using Request.Cookies["cookiename"].Value.ToString(

    Try either of these approaches

    1

    if(Request.Cookies["cookieName"] != null
    Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value)

    2
    if(Request.Cookies["cookieName"] != null

    HttpCookie aCookie = Request.Cookies["cookieName"]
    Label1.Text = Server.HtmlEncode(aCookie.Value)


    Before trying to get the value of a cookie, you should make sure that the cookie exists
    if the cookie does not exist, you will get a NullReferenceException exception

    Notice also that the HtmlEncode method was called to encode the contents of a cooki
    before displaying it in the page. This makes certain that a malicious user has not adde
    executable script into the cookie


    Juan T. Llibre, asp.net MV
    asp.net faq : http://asp.net.do/faq
    foros de asp.net, en espa?ol : http://asp.net.do/foros
    =====================================
    <> wrote in message news:...

    On Tuesday, May 06, 2008 1:37 PM
    Juan T. Llibre wrote:

    Re: ASP NET 2.0 Cookie
    re
    !> I know how to read the cookie
    !> The problem is when I try to read it in C# it always returns null

    Then, either the code you're using doesn't read the cookie properly
    or the cookie isn't being set correctly

    Have you tried the code I posted to read cookies

    As for setting cookies, here's 2 sample C# code fragments

    1
    Response.Cookies["userName"].Value = "patrick"
    Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1)

    2
    HttpCookie aCookie = new HttpCookie("lastVisit")
    aCookie.Value = DateTime.Now.ToString()
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);

    How are you setting the cookie ?
    Are you expiring the cookie ?



    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espa?ol : http://asp.net.do/foros/
    ======================================
    <> wrote in message news:...

    On Tuesday, May 06, 2008 1:42 PM
    Patrice wrote:

    I'm really not sure you can do that... ASP And ASP.NET are separate engines...
    I'm really not sure you can do that... ASP And ASP.NET are separate
    engines... I'm even surprised you don't get an exception as from the point
    of view of the C# utility (how do you run this one ? This is exposed as a
    com object ?) there is IMO no current (ASP.NET) request...

    You may want to elaborate a bit on your architecture and/or what you are
    trying to do...

    --
    Patrice

    <> a ?crit dans le message de groupe de discussion :
    ...

    On Tuesday, May 06, 2008 1:50 PM
    Patrice wrote:

    Or do you mean you just transfer control from an ASP page to an ASPX page as
    Or do you mean you just transfer control from an ASP page to an ASPX page as
    Juan seems to have understood from your description ?

    As you talked about a "C# utility" rather than about an ASP.NET page, I was
    thinking that this "C# utility" was directly called from the ASP page during
    the same request. As i said earlier you may want to elaborate a bit on your
    architecture so that we are 100 % sure about how ASP and ASP.NET interacts
    in your design...

    --
    Patrice

    "Patrice" <http://www.chez.com/scribe/> a ?crit dans le message de groupe de
    discussion : ...

    On Tuesday, May 06, 2008 2:48 PM
    Juan T. Llibre wrote:

    Cookies should be able to read regardlessof the language/platform used to
    Cookies should be able to read regardless
    of the language/platform used to write them.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espa?ol : http://asp.net.do/foros/
    ======================================
    "Patrice" <http://www.chez.com/scribe/> wrote in message news:D...

    On Tuesday, May 06, 2008 6:25 PM
    Juan T. Llibre wrote:

    Typo alert...
    Typo alert...

    That should have been :

    Cookies should be able to be read regardless
    of the language/platform used to write them.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espa?ol : http://asp.net.do/foros/
    ======================================
    "Juan T. Llibre" <> wrote in message news:%...

    On Tuesday, May 06, 2008 7:15 PM
    Mark Rae [MVP] wrote:

    Re: ASP NET 2.0 Cookie
    Correct, though some languages like C# are case-sensitive, which may be the
    issue here...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net

    On Wednesday, May 07, 2008 8:08 PM
    Peter Bromberg [C# MVP] wrote:

    Make sure the Path property of your cookie is set. , e.g. "/".
    Make sure the Path property of your cookie is set. , e.g. "/". There are
    minor inconsistencies in classic ASP cookies and ASP.NET cookies.
    Peter

    On Wednesday, May 07, 2008 8:12 PM
    Peter Bromberg [C# MVP] wrote:

    Re: ASP NET 2.0 Cookie
    To be more specific:

    Cookie Interop - ASP -->ASPX:

    There is a change in cookie behavior between ASP and ASPX. In brief: If you
    do NOT
    specifically set a cookie path, ASP will automatically set the path to:
    your web application's name --
    "/AppName"
    If you do NOT specifically set a path, ASPX will automatically set the path
    to:
    your server name --
    "/"

    To make ASP behave like ASPX, then add --
    Response.Cookies("...").Path = "/" -- to your ASP code

    To make ASPX behave like ASP, then add --
    MyCookieObject.Path = "/AppName" -- to your ASPX code.

    --Peter
    <> wrote in message
    news:...

    On Thursday, May 08, 2008 7:08 AM
    icanhelp3 wrote:

    ASP NET 2.0 Cookie
    I have a C# utility that runs in ASP website. When user logs in the
    ASP code writes the cookie using Response.Cookie. I try to read the
    same cookie in C#. I am unable to read it in C# code.

    I am using Request.Cookies["cookiename"].Value.ToString(). I can read
    the same cookie in ASP code.
    If I say Request.Cookies("CookieName") in ASP that works fine..

    On Thursday, May 08, 2008 7:08 AM
    icanhelp3 wrote:

    I know how to read the cookie.
    I know how to read the cookie. The problem is when I try to read it in
    C# it always returns null. In ASP it returns the value of the cookie.


    Submitted via EggHeadCafe - Software Developer Portal of Choice
    Book Review: C# 4.0 In a Nutshell [O'Reilly]
    http://www.eggheadcafe.com/tutorial...-a2da-88dde2e6d891/book-review-c-40-in-a.aspx
    eliza, Apr 28, 2010
    #9
    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:
    1,114
  2. Ben
    Replies:
    3
    Views:
    5,798
    Steven Cheng[MSFT]
    Jun 3, 2004
  3. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    554
  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:
    431
    =?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,236
    Dominick Baier
    Apr 14, 2008
Loading...

Share This Page