I have a MVC4 web application which works perfect on my local machine when I run it in Visual Studio.
So I publish the website to a server to go live with my application. But when I try to login my application goes to the following code:
[HttpPost]
public ActionResult CheckLogin(LoginViewModel vm)
{
if (!String.IsNullOrWhiteSpace(vm.UserName) && !String.IsNullOrWhiteSpace(vm.Password))
{
User u = ...
// Get user from database with credentials
System.Web.HttpContext.Current.Session.Add("UserIdSession", u.Id);
return RedirectToAction("Validate", "Overview");
}
else
{
// process the error that the login failed
}
}
The redirect goes to the following Action:
[HttpGet]
public ActionResult Validate()
{
if (HttpContext.Current.Session["UserIdSession"] != null)
{
return View();
}
else
{
return RedirectToAction("Index", "Login");
}
}
But the problem is that the session variable does not exists and it always send me back to the loginscreen
I've found that doing a redirect after setting a session is causing this behaviour, but I couldn't find an answer to solve this issue...
So I have some questions about it:
- Why does it work on my local machine and not on a server?
- Can I modify server settings to work this way that i'm intented to do? Does it recycle stuff after a redirect?
- Can I reproduce this behaviour on my local machine like it would on the server with some settings?
- There must be an easy way to solve this or am i wrong? Do i really need to store session variables in a database or something like that?