DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: HttpContext.Items Problems

  1. #1
    Ben Merrills Guest

    HttpContext.Items Problems


    I'm wondering if anyone can shed some light on a problem I'm having related
    to Context.Items. I can write values to the collection (from within my global.asax)
    but when i come to read out the values, i get a `Object reference not set
    to an instance of an object.`

    Here's my 'Add' code:

    ----------------------------------------
    if(Request.IsAuthenticated==true)
    {
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    Context.Session.Add("AccountID", ticket.UserData);
    }
    -----------------------------------------

    Now, when i attempt to obtain that data out of the collection in another
    page (same request), it throws the error mentioned above. My code in `myaccounts.aspx.cs`
    is:

    -----------------------------------------
    MyAccounts.DataSource = DataFormat(BillingSystemDB.GetAccounts((int)HttpContext.Current.Items["AccountID"]));
    -----------------------------------------

    Can anyone tell me why i might be getting that error? In the docs it does
    say that Context.Items only supports `get { }`. I know the IBuySpy portal
    uses this exact method to store portal information.

    Any help would be much appreciated,

    Kind Regards

    Ben Merrills
    Griffin Internet

    http://muad.xdev.net

  2. #2
    Russell Jones Guest

    Re: HttpContext.Items Problems

    Are you sure that ticket.UserData actually contains a value? If it's
    Nothing, you'll get Nothing back. Try displaying the value of
    ticket.UserData in the line before you store it into the Session. Here's
    what the documentation says about the UserData property:

    "Property Value
    The user-defined string stored in the cookie.

    Remarks
    This field will be empty ("") if no data is stored in the cookie.



    "Ben Merrills" <ben@griffin.net.uk> wrote in message
    news:3f44b5d9$1@tnews.web.devx.com...
    >
    > I'm wondering if anyone can shed some light on a problem I'm having

    related
    > to Context.Items. I can write values to the collection (from within my

    global.asax)
    > but when i come to read out the values, i get a `Object reference not set
    > to an instance of an object.`
    >
    > Here's my 'Add' code:
    >
    > ----------------------------------------
    > if(Request.IsAuthenticated==true)
    > {
    > FormsAuthenticationTicket ticket =

    FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    > Context.Session.Add("AccountID", ticket.UserData);
    > }
    > -----------------------------------------
    >
    > Now, when i attempt to obtain that data out of the collection in another
    > page (same request), it throws the error mentioned above. My code in

    `myaccounts.aspx.cs`
    > is:
    >
    > -----------------------------------------
    > MyAccounts.DataSource =

    DataFormat(BillingSystemDB.GetAccounts((int)HttpContext.Current.Items["Accou
    ntID"]));
    > -----------------------------------------
    >
    > Can anyone tell me why i might be getting that error? In the docs it does
    > say that Context.Items only supports `get { }`. I know the IBuySpy portal
    > uses this exact method to store portal information.
    >
    > Any help would be much appreciated,
    >
    > Kind Regards
    >
    > Ben Merrills
    > Griffin Internet
    >
    > http://muad.xdev.net




  3. #3
    Ben Merrills Guest

    Re: HttpContext.Items Problems


    After a little investigation, the problems seems to be a little more odd.
    Here is the code that adds the Context Item:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    if(Request.IsAuthenticated==true)
    {
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    Response.Write("User Data:" + ticket.UserData);
    Context.Items.Add("AccountID", ticket.UserData);
    }
    }

    Now, Request.IsAuthenticated is returning False here - however in further
    use later in the request (i.e. in my `myaccounts.aspx.cs`) it returns true.
    Has the value of IsAuthenticated only set after the Event Application_BeginRequest
    ?

    Ben Merrills

    "Russell Jones" <arj1@nospam.northstate.net> wrote:
    >Are you sure that ticket.UserData actually contains a value? If it's
    >Nothing, you'll get Nothing back. Try displaying the value of
    >ticket.UserData in the line before you store it into the Session. Here's
    >what the documentation says about the UserData property:
    >
    >"Property Value
    >The user-defined string stored in the cookie.
    >
    >Remarks
    >This field will be empty ("") if no data is stored in the cookie.
    >
    >
    >
    >"Ben Merrills" <ben@griffin.net.uk> wrote in message
    >news:3f44b5d9$1@tnews.web.devx.com...
    >>
    >> I'm wondering if anyone can shed some light on a problem I'm having

    >related
    >> to Context.Items. I can write values to the collection (from within my

    >global.asax)
    >> but when i come to read out the values, i get a `Object reference not

    set
    >> to an instance of an object.`
    >>
    >> Here's my 'Add' code:
    >>
    >> ----------------------------------------
    >> if(Request.IsAuthenticated==true)
    >> {
    >> FormsAuthenticationTicket ticket =

    >FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    >> Context.Session.Add("AccountID", ticket.UserData);
    >> }
    >> -----------------------------------------
    >>
    >> Now, when i attempt to obtain that data out of the collection in another
    >> page (same request), it throws the error mentioned above. My code in

    >`myaccounts.aspx.cs`
    >> is:
    >>
    >> -----------------------------------------
    >> MyAccounts.DataSource =

    >DataFormat(BillingSystemDB.GetAccounts((int)HttpContext.Current.Items["Accou
    >ntID"]));
    >> -----------------------------------------
    >>
    >> Can anyone tell me why i might be getting that error? In the docs it does
    >> say that Context.Items only supports `get { }`. I know the IBuySpy portal
    >> uses this exact method to store portal information.
    >>
    >> Any help would be much appreciated,
    >>
    >> Kind Regards
    >>
    >> Ben Merrills
    >> Griffin Internet
    >>
    >> http://muad.xdev.net

    >
    >



  4. #4
    Russell Jones Guest

    Re: HttpContext.Items Problems

    That's it. This page contains a table showing the event sequence.

    http://msdn.microsoft.com/architectu...SecNetAP04.asp

    "Ben Merrills" <ben@griffin.net.uk> wrote in message
    news:3f44da0d$1@tnews.web.devx.com...
    >
    > After a little investigation, the problems seems to be a little more odd.
    > Here is the code that adds the Context Item:
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > if(Request.IsAuthenticated==true)
    > {
    > FormsAuthenticationTicket ticket =

    FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    > Response.Write("User Data:" + ticket.UserData);
    > Context.Items.Add("AccountID", ticket.UserData);
    > }
    > }
    >
    > Now, Request.IsAuthenticated is returning False here - however in further
    > use later in the request (i.e. in my `myaccounts.aspx.cs`) it returns

    true.
    > Has the value of IsAuthenticated only set after the Event

    Application_BeginRequest
    > ?
    >
    > Ben Merrills
    >
    > "Russell Jones" <arj1@nospam.northstate.net> wrote:
    > >Are you sure that ticket.UserData actually contains a value? If it's
    > >Nothing, you'll get Nothing back. Try displaying the value of
    > >ticket.UserData in the line before you store it into the Session. Here's
    > >what the documentation says about the UserData property:
    > >
    > >"Property Value
    > >The user-defined string stored in the cookie.
    > >
    > >Remarks
    > >This field will be empty ("") if no data is stored in the cookie.
    > >
    > >
    > >
    > >"Ben Merrills" <ben@griffin.net.uk> wrote in message
    > >news:3f44b5d9$1@tnews.web.devx.com...
    > >>
    > >> I'm wondering if anyone can shed some light on a problem I'm having

    > >related
    > >> to Context.Items. I can write values to the collection (from within my

    > >global.asax)
    > >> but when i come to read out the values, i get a `Object reference not

    > set
    > >> to an instance of an object.`
    > >>
    > >> Here's my 'Add' code:
    > >>
    > >> ----------------------------------------
    > >> if(Request.IsAuthenticated==true)
    > >> {
    > >> FormsAuthenticationTicket ticket =

    > >FormsAuthentication.Decrypt(Context.Request.Cookies["account"].Value);
    > >> Context.Session.Add("AccountID", ticket.UserData);
    > >> }
    > >> -----------------------------------------
    > >>
    > >> Now, when i attempt to obtain that data out of the collection in

    another
    > >> page (same request), it throws the error mentioned above. My code in

    > >`myaccounts.aspx.cs`
    > >> is:
    > >>
    > >> -----------------------------------------
    > >> MyAccounts.DataSource =

    >
    >DataFormat(BillingSystemDB.GetAccounts((int)HttpContext.Current.Items["Acco

    u
    > >ntID"]));
    > >> -----------------------------------------
    > >>
    > >> Can anyone tell me why i might be getting that error? In the docs it

    does
    > >> say that Context.Items only supports `get { }`. I know the IBuySpy

    portal
    > >> uses this exact method to store portal information.
    > >>
    > >> Any help would be much appreciated,
    > >>
    > >> Kind Regards
    > >>
    > >> Ben Merrills
    > >> Griffin Internet
    > >>
    > >> http://muad.xdev.net

    > >
    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center