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?
Why not create a Base Controller which creates the Session variable in the
OnActionExecutingmethod, then make all your other Controllers inherit from this Base Controller, like so:Hope this helps!
Edit
As per your comment below here is, what I believe, you’re looking for (?):
You can replace where I set
Sidwith the SessionId generated from your service