eba
eba

Reputation: 673

asp mvc session variables

Need to store some auth information of client on server, to share between 2 pages. Dont matter how, session, cookie, tempdata, i tried everything, and nothing works, for exampe:

public ActionResult CheckIn(string pass)
    {
        if (System.Configuration.ConfigurationManager.AppSettings["pass"] == pass)
        {
            HttpContext.Session.Add("admin", "yes");
        }
        return View();
    } 
public ActionResult Helper() {
        if (HttpContext.Session["admin"] != null)
        {
            if (HttpContext.Session["admin"].ToString() == "yes")
                return PartialView("InitConfig");
            else
                return PartialView("StationLogics");
        }
        else
            return PartialView("StationLogics");
    }

and i get always null in session in helper method. what i'm doing wrong?

Upvotes: 1

Views: 4889

Answers (2)

Alexander Beletsky
Alexander Beletsky

Reputation: 19821

Have you tried HttpContext.Current.Session instead ?

ps. doing that you do is actually not good desing you should reconsider it in your application.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Are you sure that all those if conditions are met? Try with a simple example:

public ActionResult CheckIn()
{
    Session["foo"] = "bar";
    return View();
}

public ActionResult Helper() 
{
    var foo = Session["foo"]; 
    if (foo == null)        
    {
        // this won't be null if you called the CheckIn method first
    }
    return View();
}

Call the CheckIn action first to store a value in the session and then call the Helper action. Unless you have disabled session for this ASP.NET application or cookies in your browser you should get correct value.

Upvotes: 0

Related Questions