Melek
Melek

Reputation: 31

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?

Upvotes: 0

Views: 533

Answers (3)

Onur Dikmen
Onur Dikmen

Reputation: 367

If it is a .net core, use httpcontext. You can solve it using a distribution cache such as Redis. https://learn.microsoft.com/tr-tr/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0

If you want to develop a User Manager Use a thirty part nuget like Jwt. What it does is sso logic gives you a token for the user you use it

Upvotes: 1

Melek
Melek

Reputation: 31

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.

Upvotes: 0

Sulabh Agarwal
Sulabh Agarwal

Reputation: 291

Try using Session with HttpContext as below:-

HttpContext.Current.Session["iUserId"]=value;

Alternativly, you can try using TempData rather then Session.

TempData["iUserId"]=value;

Upvotes: 0

Related Questions