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
praneethaAsked:
Who is Participating?
 
raterusCommented:
Whatcha trying to save there, and why?
0
 
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

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
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
0
 
raterusCommented:
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
 
praneethaAuthor Commented:
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
 
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
0
 
raterusCommented:
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
 
praneethaAuthor Commented:
hmm thanks. so is this the only way to do it..using cookie or any other ideas.

Thank You
0
 
raterusCommented:
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
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.