如何在会话中使用会话和存储值并用于登录sysytem

时间:2017-12-08 12:43:56

标签: asp.net session

我正在尝试创建一个自定义登录过程,为此想要使用会话,但我无法找到任何来源,我如何在asp.net MVC中使用会话请指导我如何使用会话我只是检查他们该用户是否注销,以便他可以在仪表板上访问而无需再次登录。提前致谢。

Session["user"] = profile.userName;
                var admin = db.userProfiles.Where(x => x.userName == user.userName && x.password == user.password && x.admin==1).FirstOrDefault();
                if(admin==null)
                {
                    if (Session["user"] != null)
                    {
                        ViewBag.userName = user.userName;
                        return View();
                    }
                    else
                    {
                       return RedirectToAction("login");
                    }

                }
                else
                {

                    if (Session["user"] != null)
                    {
                        return RedirectToAction("index");
                    }
                    else
                    {
                       return RedirectToAction("login");
                    }

1 个答案:

答案 0 :(得分:0)

你正在重新发明轮子。您是否考虑过使用内置的身份和身份验证方案?

使用受授权保护的用户数据创建ASP.NET Core应用程序 https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data

使用ASP.NET Identity构建简单的ToDo应用程序并将用户与ToDoes相关联 https://blogs.msdn.microsoft.com/webdev/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes/