Search code examples
c#asp.net-mvcsessionsession-cookiessession-variables

Session value returned null in mvc5 asp.net


I have login controller and in this i get my session values

[HttpPost]
        public ActionResult Login(Models.AdminUsers adminUsers)
        {
            try
            {
                if (Session["iUserId"] != null || GetCookie("iUserId") != null)
                {
                    return Redirect("/Home/Index");
                }

                if (ModelState.IsValid)
                {
                    using (Data.DataClassesDataContext dc = new Data.DataClassesDataContext())
                    {
                        var resultUsers =
                            (from tableAdminUsers in dc.AdminUsers
                             where
                                tableAdminUsers.cEmail == adminUsers.cEmail &&
                                tableAdminUsers.cPassaword == new Class.AesCryption().Encryption(adminUsers.cPassaword) &&
                                tableAdminUsers.iActive == 1
                             select new Models.AdminUsers
                             {
                                 iUserId = tableAdminUsers.iUserId,
                                 cEmail = tableAdminUsers.cEmail,
                                 cUserName = tableAdminUsers.cUserName,
                                 cUserSurname = tableAdminUsers.cUserSurname,
                                 cImage = tableAdminUsers.cImage
                             }).FirstOrDefault();

                        if (resultUsers != null)
                        {
                            if (adminUsers.lBeniHatirla == false)
                            {
                                Session.Add("iUserId", resultUsers.iUserId);
                                Session.Add("cEmail", resultUsers.cEmail);
                                Session.Add("cUserName", new Class.TextLowerAndFirstUpper().Send(resultUsers.cUserName));
                                Session.Add("cUserSurname", resultUsers.cUserSurname.ToUpper());
                                Session.Add("cImage", resultUsers.cImage);
                            }
                            else
                            {
                                CreateCookie("iUserId", resultUsers.iUserId.ToString());
                                CreateCookie("cEmail", resultUsers.cEmail);
                                CreateCookie("cUserName", new Class.TextLowerAndFirstUpper().Send(resultUsers.cUserName));
                                CreateCookie("cUserSurname", resultUsers.cUserSurname.ToUpper());
                                CreateCookie("cImage", resultUsers.cImage);


                            }

                            return Redirect("/Home/Index");
                        }
                        else
                        {
                            ViewBag.iSonuc = -7;
                        }
                    }
                }
                else
                {
                    ViewBag.iSonuc = -6;
                }
            }
            catch (Exception Ex)
            {
                new Class.Log().Hata("AdminUsers", "AdminUsers_Post", Ex.Message);
            }

            return View();
        }

And i want to control to session in another controller but my session value return null. My control like this :

if (Session["iUserId"] == null && GetCookie("iUserId") == null)
        {
            return Redirect("/AdminUsers/Login");
        }

        int iUserLogin = 0;
        if (Session["iUserId"] != null && Convert.ToInt32(Session["iUserId"]) > 0)
        {
            iUserLogin = Convert.ToInt32(Session["iUserId"]);
        }
        else if (GetCookie("iUserId") != null && Convert.ToInt32(GetCookie("iUserId")) > 0)
        {
            iUserLogin = Convert.ToInt32(GetCookie("iUserId"));
        }

if (Session["iUserId"] == null && GetCookie("iUserId") == null) this row return true and redict to login page again.But i getting cookie correctly Why session value return null?

Where am I making mistakes? Can you help me?


Solution

  • There is a localization function that I use for language change, in this function I am changing the value of the culture using thread, I realized that this function resets my session value.