0

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?

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try using Session with HttpContext as below:-

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

Alternativly, you can try using TempData rather then Session.

TempData["iUserId"]=value;

2 Comments

How can I get control?
HttpContext.Current.Session["iUserId"]=value; i tryed this but underlined the "Current" in red and TempData will it keep the value throughout the session, so will it serve as session?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.