Jeffrey Blake
Jeffrey Blake

Reputation: 9699

In ASP.net, how can I use Cookies and Session from helper classes?

I handle the loading of user data in a single static class accessed by each of my aspx pages. I would like to add functionality to support Cookies and Session into this process. However, I am finding that the Response.Cookies object and the Session object are both unavailable to my util class.

Essentially, what I have now is (in it's own file):

namespace myProject
{    
    static class myUtil
    {
        public static myProject.User LoadUser()
        {
            //Look up user
        }
    }
}

What I would like to do is:

namespace myProject
{    
    static class myUtil
    {
        public static myProject.User LoadUser()
        {
            if (Session['user'] != null)
            { user = Session['user']; }
            else if (Response.Cookies['user'] != null)
            { user = Response.Cookies['user']; }
            else
            {               
               //Look up user
            }
        }
    }
}

How can I make this happen? In the current implementation, all references to Session and Response.Cookies are seen as undeclared objects.

For reference, here are the current imports for the class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.Page;
using System.Web.UI.WebControls;

Upvotes: 1

Views: 3166

Answers (5)

hackp0int
hackp0int

Reputation: 4169

You should use it like this, HttpContext.Current.Request.Cookies["user"]

Upvotes: 1

Pankaj
Pankaj

Reputation: 10095

Session in class file

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.SessionState;  
/// <summary>  
/// Summary description for GetSessionHelper  
/// </summary>  
public class SessionHelper : IRequiresSessionState  
{  
    public object GetSession(string key)  
    {  
        //check session   
        if (HttpContext.Current.Session[key] != null)  
        {  
            //return session value  
            return HttpContext.Current.Session[key];  
        }  
        else  
        {  
            //return empty string  
            return string.Empty;  
        }  
    }  
}  

Cookies in class file

 if (HttpContext.Current.Request.Cookies["CodeF"] != null)
 {
    string background = HttpContext.Current.Request.Cookies["CodeF"]["BackImage"].ToString();
        }

Upvotes: 5

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

You can get at anything in the HttpContext using the static HttpContext.Current variable. This is really not recommended though -- it ties your code directly to asp.net and makes things like writing tests a royal pain. In general, you might want to use more instance classes that you can pass in dependencies, such as your session variables you wish to access.

Upvotes: 1

CodingGorilla
CodingGorilla

Reputation: 19842

Most of these properties are exposed by the static HttpContext.Current member. See: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.current.aspx

Upvotes: 1

Antonio Bakula
Antonio Bakula

Reputation: 20693

you can use HttpContext.Current.Session and HttpContext.Current.Request, but be aware that Session is not available in all phases of http pipeline, for example if you use HttpModule Session is not assigned in BeginRequest

Upvotes: 2

Related Questions