防止MVC中的多次登录 [英] Prevent multiple logins in MVC

查看:98
本文介绍了防止MVC中的多次登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表名注册和登录,

当用户登录时,用户ID及其status = 1将存储在Logins表中,当用户注销时状态将变为0。 br />


我的问题是如何在登录时检查用户是否已经登录,

如果要检查的条件如下,我的代码





i have two table name Register and Login,
when user logs in the, user id and its status=1 will be store in Logins table and when user logs out status will become 0.

my problem is how should i check at login time that user is already logged in or not,
any if conditions to check, below is my code


public ActionResult GetLogin(Register d, Login r)
       {
           using (simpleEntities _LoginEntities = new simpleEntities())
           {
               var user = _LoginEntities.Registers.Where(u => u.Email.Equals(d.Email) && u.Password.Equals(d.Password)).FirstOrDefault();
               string message = "User already logged in";
                   //List<Login> abc = new List<Login>();
                   //bool aa = false;
                   //var user1 = _LoginEntities.Logins.Where(u => u.LogID.Equals(user.ID) && u.Status==(false)).FirstOrDefault();
                       if (user != null)
                       {

                                 //SELECT    *
                                 //  FROM    Notification n
                                 //  LEFT OUTER JOIN Acknowledgment a ON a.parent_id = n.id
                                 //      WHERE     (a.parent_id IS NULL OR a.status = @somevalue)
                           List<Login> ddd = new List<Login>();
                           ddd = (from a in db.Logins where a.LogID == user.ID where (a.Status == (false)) select a).ToList();
                           if (ddd!=null)
                           {
                               Session["ID"] = user.ID.ToString();
                               Session["Email"] = user.Email.ToString();
                               Session["Username"] = user.Fname.ToString() + "" + user.Lname.ToString();
                               return RedirectToAction("UserDashBoard");
                           }
                           else
                           {
                               return Json(message, JsonRequestBehavior.AllowGet);
                           }
                       }
                       return View();
                   }
                }





我尝试了什么:



i尝试使用if条件,首先检查寄存器表然后在登录表中但没有成功,plz帮我解决这个问题



What I have tried:

i tried with if conditions, first it checks in register table then in login table but no success, plz help me out with this

推荐答案

正如评论中所提到的,这样做是个坏主意。但是,您所要做的就是编写代码来检查数据库中的标志以查看它们是否已登录。您需要添加某种超时选项或重置选项,以便在用户关闭浏览器时不进行日志记录他们仍然有办法重新进入。
As mentioned in the comments, it is a bad idea to do it. However, all you have to do is write code to check the flag in the db to see if they are logged in. You'll want to add some sort of timeout option or reset option so that if the user does close the browser without logging out they'll still have a way to get back in.


这篇关于防止MVC中的多次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆