0

I am working on an ASP.NET MVC 5 application and I am having problem storing data to session. The value I get is always null.

Here is where I set the session:

string mail = user.Email;
string response = user.CheckEmail();
Session["email"] = mail;

I am testing the session here, It is redirecting to YYY:

if ((string)Session["mail"] != null)
{
    return RedirectToAction("PPP");
}
else
{
    return RedirectToAction("YYY");
}

Please immediate help will be appreciated. Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Izuagbala
  • 429
  • 3
  • 10
  • 22
  • I just build the project after coding. Cant really pinpoint the cause – Izuagbala Aug 09 '17 at 22:44
  • What session service are you using? Are you certain that the client is retaining a session token? – h0r53 Aug 09 '17 at 22:46
  • Possible duplicate of [What is the right time for ViewData, ViewBag, Session, TempData](https://stackoverflow.com/questions/12676924/what-is-the-right-time-for-viewdata-viewbag-session-tempdata) – Cyber Progs Aug 09 '17 at 22:50
  • You are saving the session email, but looking later for mail. The two don't match. – Chuck Aug 09 '17 at 23:18
  • Thanks for the support. I just noticed the error. Much love – Izuagbala Aug 10 '17 at 07:00

1 Answers1

8

You have typo.

Session["email"] = mail;

if ((string)Session["mail"] != null)
                    ^^^^

Session name should be email. In addition, you should not cast to string in order to check null.

if (Session["email"] != null)
Win
  • 61,100
  • 13
  • 102
  • 181