0

Is is possible to retrieve a Session variable from a different controller it was created from?

I create this in my Account controller

System.Web.HttpContext.Current.Session["Session"] = sessionGuid;

and from a different controller I'm trying

if (System.Web.HttpContext.Current.Session["Session"] != null)
{
    return Json(System.Web.HttpContext.Current.Session["Session"].ToString());
}
    return null;

which always returns null.

as a Test I added some code to my Login controller and in that part i do get the value out of it

if (System.Web.HttpContext.Current.Session["Session"] != null)
    {
        test = System.Web.HttpContext.Current.Session["Session"].ToString();
    }
        else
    {
        test = "no";
    }

i always got the actual result, not the"no" part of the code

1
  • 2
    Read this. You are probably not saving/setting the session state correctly Commented Mar 21, 2017 at 18:40

2 Answers 2

1

For some reason its different into how i have to access the session, at the other controller i changed to this code and it worked

if (HttpContext.Session["Session"] != null)
{
  return Json(HttpContext.Session["Session"].ToString());
}
return null;
Sign up to request clarification or add additional context in comments.

2 Comments

I shared a link earlier in an comment to your question. Read that to understand the reason.
There's a difference between HttpContext.Session and HttpContext.Current.Session
0

if controller is within the same website/virtual directory then YES, however if you are saying your session was created in a different site and you trying to access it within a controller which belongs to different site then you cannot.

2 Comments

both of them are created in the same site, and even though i am getting null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.