asp.net防止同一帐户重复登陆,查看在线人数

一、新建UserDal文件

namespace Dal
{
    public class UserDal
    {
        //获取管理员列表
        public List<UserInfo> GetUserList()
        {
            string sql = "select Id,UserName,UserPassword from UserInfo";
            List<UserInfo> list = new List<UserInfo>();
            using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, sql, null))
            {
                while(dr.Read())
                {
                    UserInfo model = new UserInfo();
                    model.Id = Convert.ToInt32(dr["Id"]);
                    model.UserName = dr["UserName"].ToString();
                    model.UserPassword = dr["UserPassword"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }
    }
}

二、新建UserInfo对象

namespace Dal
{
    //User表对象
    public class UserInfo
    {
        private int id;
        private string userName; 
        private string userPassword; 
        private string onLine;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }      
        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; }
        }
        public string OnLine
        {
            get { return onLine; }
            set { onLine = value; }
        }
    }
}

三、新建Login.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><Br />
        密码:<asp:TextBox ID="txtUserPassword" runat="server"></asp:TextBox><Br />
        <asp:Button ID="Button1" runat="server" Text="登陆 " οnclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


后台代码

using Dal;
using System.Web.Caching;/*注意引用*/

namespace WebOnline
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //登陆
        protected void Button1_Click(object sender, EventArgs e)
        {
            string userName = this.txtUserName.Text.Trim();
            string userPassword = this.txtUserPassword.Text.Trim();
            if (userName=="张三"&& userPassword=="123")
            {
                //设置缓存(该缓存被设置为1分钟后过期)
                HttpRuntime.Cache.Insert("AdminName", "张三", null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);
                Response.Redirect("Default.aspx");
            }
        }
    }
}

四、新建AdminList.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>    
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="rptList" runat="server">
           <HeaderTemplate>
            <table width="20%" border="0" cellspacing="0" cellpadding="0" style="background-color:Silver">
              <tr align="center">
                <th>管理员ID</th>
                <th>管理员名称</th>
                <th>是否在线</th>
              </tr>
           </HeaderTemplate>
            <ItemTemplate>
              <tr>
                <td><%#Eval("Id")%></td>
                <td><%#Eval("UserName")%></td>
                <td><%#Eval("Online")%></td>
              </tr>
            </ItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

后台代码

namespace WebOnline
{
    public partial class AdminList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            UserDal dal = new UserDal();
            List<UserInfo> list = dal.GetUserList();
            if(list!=null)
            {
                string name = HttpRuntime.Cache["AdminName"] as string;
                if (name != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].UserName == name)
                            list[i].OnLine = "在线";
                    }
                }
                this.rptList.DataSource = list;
                this.rptList.DataBind();
            }
        }
    }
}


五、Default.aspx页面代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">
    function test()
    {
        setInterval("ClientToServer()",10*1000);/*每隔30秒向服务器报个到,证明我还在线*/
    }  
    function ClientToServer()
    {
        $.get('Default.aspx?mytype=111&date='+new Date());/*采用jquery的post方法来提交ajax请求*/
    }
    </script>
    <style type="text/css">
    html body{ height:100%;}
    </style>
</head>
<body οnlοad="test()"><!--打开该页面后就执行test()方法-->
    <form id="form1" runat="server" style="height:100%">

 <table border="0" cellpadding="0" cellspacing="0" width="100%" style="background:#EBF5FC; height:100% ; vertical-align:top">
  <tr style="height:60px">
    <td  colspan="3" style="background-color:Blue">
    </td>
  </tr>
  
  <tr>
    <td  id="mainLeft" valign="top" style="background:#FFF; width:185px; vertical-align:top; ">
	  <div style="text-align:left;width:185px;font-size:12px; background-color:Gray ">
        
        <div style="height:100%">
          <div style="height:100%; vertical-align:top">系统管理</div>
          <ul id="1" style="display:block; height:100%">
            <li><a href="AdminList.aspx" target="iframe1">管理员列表</a></li>
            <li><a href="RoleList.aspx" target="iframe1">角色管理</a></li>
          </ul>
        </div>   
      </div>   
	</td>
	
	<td style="width:8px; background-color:Black;  vertical-align:middle">
	</td>
	
	<td  style="width:100%; height:100%;" valign="top">
      <iframe frameborder="0" id="iframe1" name="iframe1" scrolling="auto" src="AdminList.aspx" style="height:100%; width:100%;"></iframe>
	</td>
  </tr>
  
  <tr style="height:28px;">
    <td height="28px" colspan="3" bgcolor="#EBF5FC"></td>
  </tr>  
</table>
</form>
</body>
</html>

后台代码

using System.Web.Caching;

namespace WebOnline
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["mytype"] != null && Request["mytype"] == "111")
                {
                    GetClientMessage();
                }
            }
        }


        //对客户端传来的报到做相应处理
        private void GetClientMessage()
        {
            //重置缓存(该缓存被设置为1分钟后过期)
            HttpRuntime.Cache.Insert("AdminName", "张三", null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);
        }

    }
}

六、项目结构图和数据表结构图

七、登陆--->Defalut.aspx页--->再单独打开AdminList.aspx页

 八、说明

当关闭Default.aspx页面后,就不会有ajax向服务器报到的代码的执行,而服务器端的缓存设置了一分钟后过期,所以没有ajax提交报到来重置服务端的缓存,那么缓存一分钟后就失效了。所以当关闭Default.aspx页面过了一分钟后,再打开AdminList.aspx页面,则张三不在线了。

此方法适合像本例Default.aspx这种页面结构的并且是统计人员不多的情况下的应用程序。如果人数有几千上万,则客户端每隔30秒就是向服务端提交几千上万的请求,对服务器压力大。所以还是适合这种查看一个系统的管理是否在线或者禁止同一管理员重复登陆(在登陆进从缓存取出来判断)这样的场景,因为一个系统的管理员也就那么些人

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值