1

I have two controllers, Login and Account controllers. After the login validation I have set the value to the Session["username"] and redirected to the Account controllers' index page using Response.Redirect("../Account"). I need to access the value of Session["Username"] on the index page of Account Controller. I used @Session["Username"] on the view but value is not shown.

Does redirection cause disposal of session variables?

    public void LoginVerify(UserModel usermodel)
    {

        var dbContext = new db_restproEntities(); //class derived from DbContext
        var usernames = from username in dbContext.tbl_login select username; //read data

        List<tbl_login> users = dbContext.tbl_login.ToList();
        bool active = false;
        foreach (var x in users)
        {
            if (x.username == usermodel.username && x.password == usermodel.password)
            {
                if (x.active == true)
                {
                    active = true;
                    Session["Message"] = "";
                    Session["session_username"] = x.username;
                    Session["status"] = "TRUE";
                    Session["userid"] = x.user_id;
                     Response.Redirect("../" + x.role);

                }


            }

        }
        Session["Message"] = "Invalid Login Details";
        Response.Redirect("../Login");
    }
SoftSan
  • 2,482
  • 3
  • 23
  • 54
Ayush
  • 485
  • 2
  • 6
  • 19
  • Try this http://stackoverflow.com/questions/4381189/accessing-a-session-object-from-razor-layout-cshml – TGH Apr 22 '13 at 04:37
  • I suspect your login logic is not setting up the context properly. If you can post your login method code where you do the redirect, we would be able to help you – Subhash Dike Apr 23 '13 at 07:55

3 Answers3

0

Use the below code to display

<span>
  @HttpContext.Current.Session["username"].ToString()
</span>
Amit
  • 15,217
  • 8
  • 46
  • 68
0

As per my experience here on this app, the redirection caused the issue.

I changes the return type of login verify method from void to action result and replaced Response.Redirect("../"+x.role) with return RedirectoAction("Index",x.role)

The session variables are created and displayed now.

Ayush
  • 485
  • 2
  • 6
  • 19
-1

Does redirection cause disposal of session variables?

No.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291