Global.asax Cache

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
LVL 15
Who is Participating?
Whatcha trying to save there, and why?
praneethaAuthor Commented:
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


and in authenticate request of global.asax

                        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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

praneethaAuthor Commented:
but for some reason cache is ot availabe in global.asax and throws null reference exception
why don't you set the roles in the authcookie as well, cache isn't a very good place to put this you know...
praneethaAuthor Commented:
hmm looks good...but

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

so i guess i just have to take out FormsAuthentication.RedirectFromLoginPage
praneethaAuthor Commented:
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
Cache isn't instantiated at that point, the authenticate_request is one of the first things called by when you request a page.
praneethaAuthor Commented:
hmm thanks. so is this the only way to do it..using cookie or any other ideas.

Thank You
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
            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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.