限制一个帐户不能同时登陆。(针对于不是同一台计算机)

1登录Login.cs(主要代码)

protected void BtnLogin_Click(object sender, EventArgs e)
        {
            //string url = "http://sec.yoka.com/checkcode/check_code.php?checkkey=" + this.hdCheckCodeUrl.Value + "&checkvalue=" + this.txtValidCode.Text.Trim();
            //string checkCodeResult = PageText.GetWebContent(url);
            //if (checkCodeResult != "true")
            //{
            //    this.lbNotice.Text = "验证码错误!";
            //    this.txtValidCode.Text = "";
            //    return;
            //}
            if (txtPwd.Text.Length == 0 || txtUserName.Text.Length == 0)
            {
                this.lbNotice.Text = "请输入用户名和密码";
                return;
            }

            IWOMTrackerDBLayer.LoginState loginState = IWOMTrackerDBLayer.Common.LoginUserManager.Login(Server.UrlEncode(txtUserName.Text.Trim()), txtPwd.Text.Trim(), cbRemember.Checked, false);
            if (loginState == IWOMTrackerDBLayer.LoginState.Success)
            {
                //兼职权限问题,限制一个帐户不能同时登陆。(创建token)
                IWOMTrackerDBLayer.IWOMTrackerModel.LoginUserModel loginUser = IWOMTrackerDBLayer.Common.LoginUserManager.GetLoginUser(false);
                Session["iwomSingleUserLogin"] = SingleUserLogin.CreateToken(txtUserName.Text,loginUser.UserState);
                //
                if (Request.QueryString["adminlogin"] != null && Request.QueryString["adminlogin"] == "t")
                {
                    Response.Redirect("~/Admin/Default.aspx");
                }
                else if (Request.QueryString["trackeradminlogin"] != null && Request.QueryString["trackeradminlogin"] == "t")
                {
                    Response.Redirect("~/TrackerAdmin/Default.aspx");
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                this.lbNotice.Text = "错误的用户名或密码";
            }
        }
2.母版页主要代码

       protected void Page_Load(object sender, EventArgs e)
        {          
            loginUser = IWOMTrackerDBLayer.Common.LoginUserManager.GetLoginUser(false);
            if (loginUser.UserID > 0 && (CommonFunction.CheckManage(loginUser.UserState)
                || loginUser.UserState == IWOMTrackerDBLayer.UserState.Normal))     //权限验证
            {
                //验证TOKEN
                Hashtable userInformation = new Hashtable();
                userInformation = (Hashtable)Session["iwomSingleUserLogin"];
                if (!SingleUserLogin.ValidationToken(userInformation, loginUser.UserState))
                {
                    //先提示信息,然后再跳转!
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('一个帐户不能同时登陆,请重新登录。');window.location.href='Login.aspx';", true);
                }
             }
          }
3.singlerUserLogin.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Collections;

namespace IWOMTracker.Common
{
    public class SingleUserLogin
    {
        private static System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
        public static Hashtable CreateToken(string name)
        {
            return CreateToken(name, IWOMTrackerDBLayer.UserState.Normal);
        }
        public static Hashtable CreateToken(string name, IWOMTrackerDBLayer.UserState state)
        {
            Hashtable userInformation = new Hashtable();
            if (state == IWOMTrackerDBLayer.UserState.Normal)
            {
                string key = name + "SULToken";
                string value = Guid.NewGuid().ToString();
                userInformation.Add("USERKEY", key);
                userInformation.Add("USERVALUE", value);
                httpRuntimeCache.Insert(key, value, null, DateTime.Now.AddHours(24), TimeSpan.Zero);
            }
            return userInformation;
        }
        public static bool ValidationToken(Hashtable userInformation, IWOMTrackerDBLayer.UserState state)
        {
            if (state != IWOMTrackerDBLayer.UserState.Normal) return true;
            if(userInformation.Count==0)return false;
            string key = userInformation["USERKEY"].ToString();
            string value = userInformation["USERVALUE"].ToString();
            string token =httpRuntimeCache[key]==null?"":httpRuntimeCache[key].ToString();
            if (state == IWOMTrackerDBLayer.UserState.Normal && value != token)

            {
                return false;
            }
            else
            {
                return true;
            }
            
        }
        public static void RemoveToken(Hashtable userInformation, IWOMTrackerDBLayer.UserState state)
        {
            string key = userInformation["USERKEY"].ToString();
            httpRuntimeCache.Remove(key);
        }
    }
}




你可能感兴趣的:(限制一个帐户不能同时登陆。(针对于不是同一台计算机))