C# 检查用户是否已登录asp.net网站
我正在开发一个网站,用户需要登录到系统才能使用它。当前的功能是: 当用户输入用户名和密码时,将在DB中进行检查,以检查该用户是否存在以及是否输入了正确的密码。 然后才允许该用户登录 到目前为止一切都很好,现在客户端希望在日志功能中再添加一个功能,即客户端希望仅限制该用户存在一个会话 例如,如果user1是从电脑的一个浏览器登录的,则他应该 不允许从其他系统或相同浏览器登录 个人电脑 我该怎么做?我计划在我的数据库中使用一个位字段,当用户第一次登录时将设置该字段。若他第二次尝试登录,请检查该字段,并仅在未设置位字段的情况下允许登录 但我觉得这会引起一些问题 1) 如果用户错误地关闭了浏览器的选项卡并再次尝试登录,他将无法这样做,因为位字段仍将在DB中设置 2) 如果用户错误地关闭浏览器,设置字段何时会被清除 如果有任何其他方法来实现它,那么你可以自由地为我指出一个正确的方向C# 检查用户是否已登录asp.net网站,c#,sql,asp.net,sql-server-2008,C#,Sql,Asp.net,Sql Server 2008,我正在开发一个网站,用户需要登录到系统才能使用它。当前的功能是: 当用户输入用户名和密码时,将在DB中进行检查,以检查该用户是否存在以及是否输入了正确的密码。 然后才允许该用户登录 到目前为止一切都很好,现在客户端希望在日志功能中再添加一个功能,即客户端希望仅限制该用户存在一个会话 例如,如果user1是从电脑的一个浏览器登录的,则他应该 不允许从其他系统或相同浏览器登录 个人电脑 我该怎么做?我计划在我的数据库中使用一个位字段,当用户第一次登录时将设置该字段。若他第二次尝试登录,请检查该字段,
正如一些同事所指出的,这个问题有重复的地方,但这些问题并不是我真正想要的,因为
他们使用基于表单的身份验证,而我不是您可以向userTable添加一列,如IsLoggedIn
。在登录时,您可以在global上的Session\u End
事件中更改为true。asax
将其更改为false不要试图通过将值存储在database中来解决此类问题,而应采用类似于Session的方法
在登录页面或默认页面中,选择使用会话登录的用户(即会话[“用户名”]=“某些登录用户”)
现在,在加载每个manager页面时,检查会话的有效性,如下所示
if(!mCheckStatusOfSession())
{
Response.Redirect("To Login Page")
}
protected void mCheckStatusOfSession{
if (Application[Session["UserName"].ToString()] == null)
{
return false;
}
else
{
Application[Session["UserName"].ToString()].ToString();
if (strID == Session.SessionID.ToString())
{
return true;
}
else
{
return false;
}
}
}
1) 用户登录时,我们使用用户名+密码作为缓存项的密钥来检查缓存。如果缓存项存在,我们知道登录名已经在使用中,所以我们将其踢出。否则,我们将对它们进行身份验证(数据库等),并让它们进入
2) 让他们进入后,我们设置一个新的缓存项条目,该条目的密钥由他们的用户名+密码组成,滑动过期时间等于当前会话超时值。我们还可以设置一个新的会话变量Session[“user”],该变量的值为username+password,这样我们就可以在用户会话期间对每个页面请求进行连续的页面请求检查和缓存更新。这为我们提供了“复制”缺少的会话结束功能的基础结构
3) 现在我们需要一种方法来更新每个页面请求的缓存过期时间。您可以在全局中的应用程序_PreRequestHandlerExecute处理程序中非常优雅地执行此操作,因为会话对象在该处理程序中可用且“活动”。此外,每个页面请求都会触发此事件,因此我们不需要在任何页面中放入一行额外的代码。我们使用Session[“user”]值获取该用户的密钥以检索其缓存项,从而重置它并自动将滑动过期设置为新的超时值。无论何时访问缓存项,其SlidingExpiration属性(如果正确配置)都会自动更新。当用户放弃会话并且在一段时间内没有请求页面时,其缓存项的滑动过期最终过期,并且该项将自动从缓存中删除,从而允许具有相同用户名和密码的用户再次登录。不要大惊小怪,不要胡闹!可用于InProc、StateServer和SQL Server会话模式
参考文章:,要详细了解我的评论:
步骤1
创建包含以下字段的Sessions
表:
SessionId ( Primary Key ) char(24)
UserId ( Foreign Key to Users table ) int
LoginDate datetime
步骤2
创建您的会话
类
public class Session {
public string Sessionid { get; set; }
public int UserId { get; set; }
public DateTime LoginDate { get; set; }
}
步骤3
如果您有一个名为DoLogin
的函数
public void DoLogin() {
//validation commes here...
//create your session
Session["User"] = user; //user is your User class object
//create session class for db
Session session = new Session();
session.SessionId = ""; //you can generate here a 24 character string
session.UserId = user.Id;
session.LoginDate = DateTime.Now;
db.Add(session); //add session to db
}
步骤4
创建一个函数来检查用户是否已经登录
public bool IsLoggedIn(User user) {
Session session = db.GetSession(user.Id); //Get session of the user
if(session != null)
{
return true;
} else {
return false;
}
}
这就是我在@Satinder singh
提供的帮助下如何做到的
Global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
Application["UsersLoggedIn"] = new System.Collections.Generic.List<string>();
}
protected void Session_End(object sender, EventArgs e)
{
// NOTE: you might want to call this from the .Logout() method - aswell -, to speed things up
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn.Length > 0)
{
System.Collections.Generic.List<string> d = Application["UsersLoggedIn"]
as System.Collections.Generic.List<string>;
if (d != null)
{
lock (d)
{
d.Remove(userLoggedIn);
}
}
}
}
protected bool Login(string userId)
{
System.Collections.Generic.List<string> d = Application["UsersLoggedIn"]
as System.Collections.Generic.List<string>;
if (d != null)
{
lock (d)
{
if (d.Contains(userId))
{
// User is already logged in!!!
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn == user_id)
{
Session["UserLoggedIn"] = user_id;
return true;
}
else
{
return false;
}
}
else
{
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn != user_id)
{
d.Add(userId);
}
}
}
}
Session["UserLoggedIn"] = userId;
return true;
}
受保护的无效应用程序\u启动(对象发送方,事件参数e)
{
Application[“UsersLoggedIn”]=new System.Collections.Generic.List();
}
受保护的无效会话\u结束(对象发送方,事件参数e)
{
//注意:您可能还想从.Logout()方法调用它,以加快速度
字符串userLoggedIn=Session[“userLoggedIn”]=null?string.Empty:(string)Session[“userLoggedIn”];
如果(userLoggedIn.Length>0)
{
System.Collections.Generic.List d=应用程序[“UsersLoggedIn”]
as System.Collections.Generic.List;
如果(d!=null)
{
锁(d)
{
d、 删除(userLoggedIn);
}
}
}
}
Login.aspx.cs
protected void Application_Start(object sender, EventArgs e)
{
Application["UsersLoggedIn"] = new System.Collections.Generic.List<string>();
}
protected void Session_End(object sender, EventArgs e)
{
// NOTE: you might want to call this from the .Logout() method - aswell -, to speed things up
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn.Length > 0)
{
System.Collections.Generic.List<string> d = Application["UsersLoggedIn"]
as System.Collections.Generic.List<string>;
if (d != null)
{
lock (d)
{
d.Remove(userLoggedIn);
}
}
}
}
protected bool Login(string userId)
{
System.Collections.Generic.List<string> d = Application["UsersLoggedIn"]
as System.Collections.Generic.List<string>;
if (d != null)
{
lock (d)
{
if (d.Contains(userId))
{
// User is already logged in!!!
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn == user_id)
{
Session["UserLoggedIn"] = user_id;
return true;
}
else
{
return false;
}
}
else
{
string userLoggedIn = Session["UserLoggedIn"] == null ? string.Empty : (string)Session["UserLoggedIn"];
if (userLoggedIn != user_id)
{
d.Add(userId);
}
}
}
}
Session["UserLoggedIn"] = userId;
return true;
}
受保护的bool登录(字符串userId)
{
System.Collections.Generic.List d=应用程序[“UsersLoggedIn”]
as System.Collections.Generic.List;
如果(d!=null)
{
锁(d)
{
if(d.Contains(userId))
{
//用户已登录!!!
字符串userLoggedIn=Session[“userLoggedIn”]=null?string.Empty:(string)Session[“userLoggedIn”];
if(userLoggedIn==user\u id)
{
会话[“UserLoggedIn”]=用户id;
返回true;
}
其他的
{
返回f