Click to See Complete Forum and Search --> : HttpContext.Items Problems


Ben Merrills
08-21-2003, 08:06 AM
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

Russell Jones
08-21-2003, 10:22 AM
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

Ben Merrills
08-21-2003, 10:41 AM
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
>
>

Russell Jones
08-21-2003, 11:09 AM
That's it. This page contains a table showing the event sequence.

http://msdn.microsoft.com/architecture/application/default.aspx?pull=/library/en-us/dnnetsec/html/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
> >
> >
>