如题:防止同时登陆。(asp.net)
关于防止同时登录的问题,看了很多帖子,大多都是只提到了application,session 或者是通过数据库中的字段的状态来判断。但是都没有具体有用的实例或代码。
本人再次向各位大侠赐教,小弟不胜感激
关于防止同时登录的问题,看了很多帖子,大多都是只提到了application,session 或者是通过数据库中的字段的状态来判断。但是都没有具体有用的实例或代码。
本人再次向各位大侠赐教,小弟不胜感激
解决方案 »
- 网站整合discuz论坛
- 一个简单ASP.NET问题
- GridView 嵌套一个Label,我想在label失去焦点时触发一个事件。怎么实现?(在线等)
- 乱码问题!fxqyyzg(海冬青)请再来帮帮忙
- VS05中的用户控件没VS03中的用户控件好用?大家在没有同感
- 网站设计
- datalist中的ItemCommand不响应,请大虾指点
- .net 导出Excel 并带有控件(checkbox等)
- 急求教一个linq语句问题。。。。。
- 读取html页面,怎样把html页面中的控件显示出来?是帅哥的麻烦都进来楸楸,送美女
- 后台.ne如何序列化为json格式
- 求水晶报表10+vs2003的服务器安装包cr10_net_server.exe
string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
if (uer == null || uer == String.Empty)
{
//定义cache过期时间 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0); //第一次登陆的时候插入一个用户相关的cache值,
HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
Session["ADMINID"] = TextBox1.Text;
Response.Redirect("main.aspx");
}
else
{
//重复登陆
Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
}
Session[“UserInfo”] = user.GetUserInfoByUName(userName, ref error);Session[“LastLoginDate”].LastLoginDate = dt;
//这里把该用户名与登陆时间存到登陆用户列表中:IList al = new ArrayList();
//这里我加的是用户名,因为这里是用用户名做主键的,其实用int更好.如果你是用int做用户主键,请用userid.
al.Add(txtUserName.Text);al.Add(dt);LoginInfo.Add(al);页面里增加脚本,每隔一段时间(5秒)取一下状态,看看这个用户名是不是在其他地方登陆了,这里用到了prototype的ajax:<script language='javascript' type='text/javascript'>"function ValidSimpleUser(){new Ajax.Request('LeadNT.aspx?op=otherLogin', {onSuccess: function(transport) {if(transport.responseText == 1')top.location.href=''LeadNT.aspx?op=logout';}}); }setInterval('ValidSimpleUser();',5000);</script>脚本中用到了一个页面LeadNT.aspx,第一次调用返回一个值,如果是1 ,则表示别人登陆这个帐号了,第二次调用,是用这个页面来注销当前用户:
LeadNT.aspx中的部分代码:
//第一次调用:if (Request.QueryString["op"] == "otherLogin"){if (!string.IsNullOrEmpty(CUser.UserName)){foreach (IList li in LoginInfo){
//此处是伪代码,如果自己写没这么复杂.一个实体类即可.
if (li[0].ToString() == ((UserInfo)Session["UserInfo"]).UserName && DateTime.Parse(li[1].ToString()) > DateTime.Parse(Session["LastLoginDate"].ToString()){LoginInfo.Remove(li);Response.Write("1");break;return;}}}}
//第二次调用if (Request.QueryString["op"] == "logout"){Response.Write("<script language='javascript' type='text/javascript'>alert('该帐号已在其他地方登录!');top.location.href=' "Logon.aspx?logout=true';</script>");}
http://www.cnblogs.com/caojinqin/archive/2008/11/04/1326275.html
http://www.chenjiliang.com/Article/View.aspx?ArticleID=13417&TypeID=5
用户多时是不是对服务器要求较高?
//登录
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
DBConnClass loginDB = new DBConnClass();
string passw = DBConnClass.Get_MD5_Method(this.TextBox1.Text.ToString().Trim());
string login = loginDB.UserLogin(txtname.Text.Trim(), passw);
if (login != null)
{
string key = txtname.Text;
string uer = Convert.ToString(Cache[key]);
if (uer == null || uer == String.Empty)
{
TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
Session["UsersName"] = txtname.Text.Trim();
if (ViewState["OldUrl"] != null)
{
string url = ViewState["OldUrl"].ToString();
ViewState["OldUrl"] = null;
Response.Redirect(url);
}
else
{
Response.Redirect("index.aspx");
}
}
else
{
Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
}
}
else
{
msg.Text = "<script>alert(\"登录失败!用户名或密码错误!\")</script>";
}
}这样写了但是每次都是直接提示 “您的账号已经登陆!” 难道那代码不是放在登录按钮的事件中吗?
{
if(Session["username"]!=null)
{
Response.Write("<script>alert('你已经登录');</script>");
return;
}
}
btnLogin_Click
{
if(用户名密码正确)
{
Session["username"] = "用户名";
}
}
string strCacheKey = this.TextBox1.Text;
strUser = Convert.ToString(Cache[strCacheKey]);
if(strUser == string.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
Cache.Insert(strCacheKey,strCacheKey,null,DateTime.MaxValue,SessTimeOut,CacheItemPriority.NotRemovable,null);
Session["User"] = strCacheKey;
this.Label1.Text = Session["User"].ToString();
}
else
{
Response.Write("<script>alert('这个用户已经登录!')</script>");
}
貌似global.asax里有会话结束时间...
先判断是否为空
然后 再判断 登陆的用户 时候和存储的用户 一致 如果一致 那么就拒绝登陆
这里只是判断cache中有没有当前用户信息,不需要做转换Convert.ToStringstring key = txtname.Text; if (Cache[key] == null){
}
如果session已经存在该用户名,提示,不让登陆
create proc useronlylogon
@userId nvarchar(100)='testuser'
as
begin
if not(object_id('tempdb.dbo.#tmp' + @userId) is null)
begin
select 0
end
else
begin
declare @sql nvarchar(200)
select @sql='create table #tmp' + @userId + '(UserName nvarchar(100))'
exec(@sql)
select 1
end
end
登陆前用该存储过程去判断,返回为0表示该用户应经登陆,为1表示为登陆。log off的时候把这个临时表删除掉,临时表在服务器 down机的时候自动删除,这样比那种设置数据库字段更安全。
alter proc useronlylogon
@userId nvarchar(100)='testuser'
as
begin
if not(object_id('tempdb.dbo.##tmp' + @userId) is null)
begin
select 0
end
else
begin
declare @sql nvarchar(200)
select @sql = 'create table ##tmp' + @userId + '(UserName nvarchar(100)) ' + char(13)
exec(@sql)
select 1
end
end
你可以用我在46楼里面写的存储过程,这样就不存在 session的问题了,即使即使数据库服务器down机也不会影响用户下次登陆
设置一个字段,当用户登录时,置这个字段为登录状态,当用同一个用户名再次登录时,检测字段状态是否是登录
if(是)
{
拒绝登录;
}
else
{
登录;
}
http://topic.csdn.net/u/20081023/09/9df24217-94a1-4c9b-81c6-911f5ff6de1a.html如果想要代码的话把邮箱留下
已发送:[email protected] --Hubery
当用户登录成功的时候 就随机生成一个加密的临时密码并保存在RandPwd中
同时保存在本地的Cookie里面
在验证用户是否登录的时候 比对临时密码 看是否一样
不一样就说明已经在其他地方登录了
这样子的方法有个限制 就是用户在同一时刻只能在一个地方登录
在同一台机器上 你用IE登录后 再用FF登录也是不能同时登录的
[email protected]