[RESOLVED]Session key value returns null

Hi,

I do Session.Add("key", value) from a controler, but when i post a form session["key"] returns null?

Session mode in web.config is SQLServer. Timeout is default value – 20 minutes. What’s wrong?

Nobody knows?

i’m trying to save a confirmation code when a user register. May be another way exists?

Ever tried?:

Session["key"] = value

Does the code work if you use InProc for session?  If so it is probably that your session config isn’t properly setup for use SQL as storage.

I do this:

if (Session["key"] == null)
{
   var key = (new Random()).Next(100000, 999999);
   Session.Add("key", key);
   return View(registerView);
}

I post my form twice

In the first case i set a key. In the second case i check a key which should not be null but is

No, InProc doesn’t work, that’s why i try to use SQLServer.

If I look in the database, i can see an item for my test session

Where are you reading the session?

I set a value in c# code and send the form to the client. User entering a confirmation key and post the form again. In controller I want to check key, to do this I need get a key from the session and compare with the entered key but session["key"] is null

By the way if i write:

    <div class="middle">
        <div class="form-input form-input-confirm-key">
            @Html.TextBox("_ConfirmKey", "", new {id="_ConfirmKey", placeholder = @GlobalRes.ConfirmKey })
        </div>
        <div class="help-context">
            @GlobalRes.HelpConfirmKeyMessage
        </div>
        @Session["key"]
    </div>

I can see a value on the client side

This statement in fact runs at server. So, that means, the server session worked.

@Session["key"]

so why i can’t get key value from c# code?

If it’s working in razor (which executes on serverside), the same should work in C#. Shall we know where exactly you are trying to access Session["key"] – Was it in some Controller Action?

Yes, that’s right:

[HttpPost]
public ActionResult Register(RegisterView registerView)
{
    if (ModelState.IsValid)
    {
        if (Session["key"] == null)
        {
            var key = (new Random()).Next(100000, 999999);
            Session.Add("key", key);
            return View(registerView);
        }

        // TODO: Check a confirmation key
        // TODO: Add a database user item

        return RedirectToAction("Index", "Profile");
    }

    return View(registerView);
}

BrockAllen once pinpointed me to this, after I asked him a question about Session[]:

http://brockallen.com/2012/04/07/think-twice-about-using-session-state/

Could save you some trouble.

I know that InProc mode is awful. That’s why i use SQL Server mode.

I need to save confirmation key which can’t be cached or stored on the client side cause of safety

sameness

[HttpPost]
public ActionResult Register(RegisterView registerView)
{
    if (ModelState.IsValid)
    {
        if (Session["key"] == null)
        {
            var key = (new Random()).Next(100000, 999999);
            Session.Add("key", key);
            return View(registerView);
        }

        // TODO: Check a confirmation key
        // TODO: Add a database user item

        return RedirectToAction("Index", "Profile");
    }

    return View(registerView);
}

Are you sure setting the session key should be in that HttpPost action - shouldn’t it be in a HttpGet action? They way you have it now will display the page again after posting, because it wasn’t set yet.

I’m sure. At first I need check a model "isvalid" then create a key and return the same form with popup for confirmation key

If your model is like this    

    public class RegisterView
    {
        //other profile props
        public string ConfirmKey { get; set; }//it can also be integer
    }

Main view, make it ajax form. show that you can render partial view replace same container of current form first post for confirmation key

 [HttpPost]
        public ActionResult Register(RegisterView registerView)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(registerView.ConfirmKey))
                {
                    var key = (new Random()).Next(100000, 999999);
                    Session.Add("key", key);
                }
                else
                {
                    if (registerView.ConfirmKey == Session["key"].ToString()) // TODO: Check a confirmation key
                    {
                        //Add a database user item
                        return RedirectToAction("Index", "Profile");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid confirmation key");
                    }
                }
               
            }

            return PartialView(registerView);
        }

Leave a Reply