?
Solved

Global.asax Cache

Posted on 2005-05-09
10
Medium Priority
?
751 Views
Last Modified: 2008-02-01
i am trying to use

Cache["key"] in the global.asax authenticate request...and it throws object null reference exception..hmmm

can any one help me with it...

i set a cache object in .aspx and i am using it in authenticate_request ( i thought we cant use session so i am using cache)

Thank You
0
Comment
Question by:praneetha
  • 5
  • 5
10 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 13963195
Whatcha trying to save there, and why?
0
 
LVL 15

Author Comment

by:praneetha
ID: 13963240
well i have a login.aspx page ..where user enters his username and password and at that point i store his username in authcookie and roles in cache object

FormsAuthentication.SetAuthCookie(username,false);
                        Context.Cache[User.Identity.Name+"Roles"]=role;

and in authenticate request of global.asax

if(System.Web.HttpContext.Current.User!=null)
                  {
                        GenericIdentity gid=new GenericIdentity(System.Web.HttpContext.Current.User.Identity.Name);
                        string[] MyString = {Context.Cache[User.Identity.Name+"Roles"].ToString()};//{"Administrator"};
                        GenericPrincipal gpr=new GenericPrincipal(gid,MyString);
                  

that's what i am trying to do
0
 
LVL 15

Author Comment

by:praneetha
ID: 13963245
but for some reason cache is ot availabe in global.asax and throws null reference exception
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 33

Expert Comment

by:raterus
ID: 13963285
why don't you set the roles in the authcookie as well, cache isn't a very good place to put this you know...
0
 
LVL 33

Accepted Solution

by:
raterus earned 2000 total points
ID: 13963306
0
 
LVL 15

Author Comment

by:praneetha
ID: 13963326
hmm looks good...but

 // Don't call FormsAuthentication.RedirectFromLoginPage since it could
   // replace the authentication ticket (cookie) we just added
   Response.Redirect(returnUrl);

so i guess i just have to take out FormsAuthentication.RedirectFromLoginPage
0
 
LVL 15

Author Comment

by:praneetha
ID: 13963356
so do u know why is teh cache not working..well i know it is not the good way to do..but just curious why isnt working
0
 
LVL 33

Expert Comment

by:raterus
ID: 13963371
Cache isn't instantiated at that point, the authenticate_request is one of the first things called by asp.net when you request a page.
0
 
LVL 15

Author Comment

by:praneetha
ID: 13963435
hmm thanks. so is this the only way to do it..using cookie or any other ideas.

Thank You
0
 
LVL 33

Expert Comment

by:raterus
ID: 13963483
usually, later in the lifecycle, when the session has been instantiated, I have a "LoggedOnUser" object, which can recreate itself if need be, you can use values from the cookie to do this too, but here is a simple example.

    Protected Readonly Property LoggedInUser() As MyCustomUser
        Get
            If Session("LoggedInUser") Is Nothing Then
                Dim usr As MyCustomUser= New MyCustomUser(User.Identity.Name, "Invest")
                Session("LoggedInUser") = usr
            End If

            Return DirectCast(Session("LoggedInUser"), MyCustomUser)
        End Get
    End Property
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 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