Search code examples
asp.net-mvcsessionsession-cookiessession-state

MVC RedirectToAction calls Session_Start() in global.asax


I am calling RedirectToAction method to call action method from another controller but it clears all session data I debugged in global.asax and found out that when ever i call RedirectToAction it calls Session_Start() method. I don't know how session start is called. Here is my web config code for form and session tag

for session tag

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
  </providers>
</sessionState>

and for form tag

<authentication mode="Forms">
  <forms loginUrl="~/ControllerName/ActionName" timeout="2880" />
</authentication>

this "~/ControllerName/ActionName" is same method from wher i am calling "RedirecToAction"

just for info what i am trying is if i found cookies for user he will be redirected to home page from login page directly

both action methods are in different controller and in different Areas. here is code which uses "RedirecToAction" method

public class LoginController : Controller{
public ActionResult Index()
{
    if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
    {
        FillLoginSession();//Fills Session with user data ex. Session["User_Id"] = 1;
        return RedirectToAction("Index", "Home", new { area = "Home" });
    }
}}

and here is action method in another controller to which i am redirecting.

public class HomeController : Controller{
public ActionResult Index()
{
    return View();
}}

Solution

  • ok i got solution to this problem. i was calling this

    Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
    

    before initializing session from my method. In my IIS this key "ASP.NET_SessionId" was used to store session in cookies. as it was cleared it reinitialize everything when ever I redirect to new page.