会话值在 mvc5 asp.net 中返回 null

天使

我有登录控制器,在这里我得到了我的会话值

[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();
        }

我想控制另一个控制器中的会话,但我的会话值返回 null。我的控制是这样的:

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) 此行返回true并再次重定向到登录页面。但我正确获取cookie 为什么会话值返回null?

我在哪里犯错?你能帮助我吗?

天使

我有一个用于语言更改的本地化函数,在这个函数中我正在使用线程更改文化的值,我意识到这个函数会重置我的会话值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Asp.net mvc5。异步等待。等待具有diff返回类型的任务

从ViewBag设置文本输入的值?ASP.NET MVC5

ASP.NET MVC5:要使用模型绑定更新Collection中的几个项目

ASP.NET MVC5:对具有会话的控制器进行单元测试

ASP.NET MVC5中的密码恢复

ASP.NET MVC5:RedirectToAction

如何在ASP.NET 5 MVC6中的布局文件中获取会话值

在ASP.NET MVC5中绑定@ Html.DropDownListFor的最佳方法是什么?

Google ASP.NET MVC5上的AuthenticationManager.GetExternalLoginInfoAsync()返回null

如何在ASP.NET MVC中检查会话值是否为空或会话密钥不存在-5

在剃刀视图文件(ASP.NET MVC5)中,@ Raw语句未知

ASP.Net MVC5中DropDownlist的困难

控制器从ASP.NET MVC5表单接收空值

在ASP.NET MVC5中显示PageList

在详细信息视图asp.net mvc5中,外部值为空

在ASP.NET MVC3中,会话项目始终为null

自定义MVC5 ASP.NET Identity中的cookie值

Asp.net MVC5引导程序:无法在编辑页面下拉列表中获取选定的值

如何在ASP.NET MVC5中编辑.cshtml文件

ASP.NET MVC 5中的Null引用异常

如何在ASP.NET MVC5上设置会话过期时间使用代码?

ASP.NET MVC5在文本框中显示字符串

在ASP.NET MVC5中调用异步方法时无法等待'long'

如何从ASP.NET MVC5中的JSON绑定模型中的属性

根据asp.net mvc5中数据库的值设置下拉列表的值

Iframe 未在 Asp .Net MVC5 中显示网站

如何在 ASP.NET MVC5 中列出从最新到最旧的帖子

在 Asp.Net mvc5 Identity 版本 1.0.0 中忘记密码

CheckBox 在 ASP.NET MVC 中总是返回 null