Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing session across controllers in C# MVC3

I'm creating a website in C# using MVC3. The website is a client that uses a web service. The web service uses a unique session id per user, which is given as the last parameter in each service call. You get the session id when you log in.

I'm able to get the session id and save it in a property in my user controller:

private string Sid
{
    get
    {
        var sid = Session["Sid"];
        return sid == null ? "" : sid.ToString();
    }
    set
    {
        Session["Sid"] = value;
    }
}

In my other controllers I'm able to get the session id with a similar property (just without the setter), but when it asks the user controller to do something where it accesses its own property to get the id the session is null.

It seems like the sessions don't get transferred between the controllers, but I don't know how to do that. I would like to access the session from one central place, instead of having properties in each controller, but I can't figure out how to do it.

After three days of searching Google hasn't been able to give me the answer. Do you have any ideas?

like image 555
Mikkel R. Lund Avatar asked Jan 16 '23 15:01

Mikkel R. Lund


2 Answers

Why not create a Base Controller which creates the Session variable in the OnActionExecuting method, then make all your other Controllers inherit from this Base Controller, like so:

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Session["Sid"] = Session.SessionID;
        base.OnActionExecuting(filterContext);
    }        
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

public class TestController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

Hope this helps!

Edit

As per your comment below here is, what I believe, you're looking for (?):

public class BaseController : Controller
{
    public string Sid
    {
        get
        {
            var sid = Session["Sid"];
            return sid == null ? "" : sid.ToString();
        }
        set
        {
            Session["Sid"] = value;
        }
    }
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        Sid = "2343432233aaaa";
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

public class TestController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.Name = Session["Sid"];
        return View();
    }
}

You can replace where I set Sid with the SessionId generated from your service

like image 199
CallumVass Avatar answered Jan 20 '23 15:01

CallumVass


The behaviour you are describing is very unexpected; the Session is definitely the same, so there is something else causing this behaviour.

However, the "usual" way to deal with this is to create a custom SessionWrapper. The SessionWrapper is a static class with public properties, and most importantly, it's the single point where you will access the session - so all the controllers will use this class for writing to/reading from the session.

A very simple version would look something like this:

public static class SessionWrapper
{
    public string Sid 
    { 
        get 
        { 
            var sid = Session["Sid"]; 
            return sid == null ? "" : sid.ToString(); 
        } 
        set 
        { 
            Session["Sid"] = value; 
        } 
    }
}
like image 42
Bogdan Banut Avatar answered Jan 20 '23 15:01

Bogdan Banut