protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String sKey = "107";     //登录用户ID号
        Boolean canLogin = false;       //判断是否能登录
        String sUser = GetString(sKey);     //取出Cookie sKey 的内容 如果为空返回 ""
        if (sUser != null && sUser != "")       //当Cookie 有值是 
        {
            canLogin = true;        //允许登录
        }
        else
        {
            sUser = Convert.ToString(Cache[sKey]);        //判断Cache中是否有登录
            if (sUser == null || sUser == String.Empty)     //如果不存在 107 允许登录
            {
                canLogin = true;
            }
        }        if (canLogin == true)       //是否能登录  以下是孟子E章的代码 用Cache记录用户是否已经登录
        {
            TimeSpan sessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
            HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, sessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            Session["sKey"] = sKey;
            HttpCookie cookiesKey = new HttpCookie(sKey, sKey);
            cookiesKey.Expires = DateTime.Now.AddMinutes(System.Web.HttpContext.Current.Session.Timeout); ;     //Cookie过期为 Session过期 不知道这里写得对不对??
            Response.Cookies.Add(cookiesKey);
            Response.Write("登录成功");
        }
        else
        {
            Response.Write("您好像已经登录了!");
            return;
        }
    }
    String GetString(String sKey)       //取得当前登录用户的Cookie
    {
        String result = "";
        try
        {
            result = Convert.ToString(Request.Cookies[sKey].Value);
        }
        catch { }
        return result;
    }
    protected void Button2_Click(object sender, EventArgs e)    //这里为测试时用
    {
        String sKey = Session["sKey"].ToString();
        Response.Cookies[sKey].Expires = DateTime.Now.AddDays(-1);
        Response.Write("清除成功!");
    }
    protected void Button3_Click(object sender, EventArgs e)    //注销用户 清除Cache 和 Cookies
    {
        if (Session["sKey"] != null)
        {
            String sKey = Session["sKey"].ToString();
            Session.Remove(sKey);
            Cache.Remove(sKey);
            Response.Cookies[sKey].Expires = DateTime.Now.AddDays(-1);
        }
    }
---------------------------------------------------------
<form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="清除Cookies测试用" />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="退出登录" />
    
    </form>
---------------------------------------------------------------------
这里是用Cookies来记录当前用户,当Cookies存在的时候允许用户当前机子重新登录
但有一个问题是
Session过期是20分钟
Cookies那个我也设置了20分钟
但当用户有操作Session的时候他会自动延时
但Cookies就还是20分种
这样就会用户过20分种以后如果不注销而关掉浏览器的话
就会要等到那个Session过期后 才能登录了还有一个就是
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, sessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
这一句我看也就是说Cache有效为20分钟,但当Session延时后这个会不会跟着延时呢
谢谢

解决方案 »

  1.   

    我这里是根据
    如果用户有这个登录的Cookie的话可以登录
    如果没有的话检查Cache看是否有这个用户的Cache
    如果有的话说明已经登录,否则还没有登录但这里有一个问题是如何来设计Cache和Cookie的过期问题
    我是设计和Session一样的时间20分钟
    但Session每当有用到都会自动延时,而Cookie和Cache这时还是那个时间
    所以过了20分钟的话 这个单点登录就不成立了 因为Cooki和Cache都消失了
      

  2.   

    技术上,我就不说了,网上有太多的文章了。我说一些费话。一个用户(A),他有正确的账户名和一个正确的密码,他算不算是合法用户??我想算吧。那么,又有一个用户(B),他也有正确的账户名和一个正确的密码,他算不算是合法用户??我想也算吧。那么,Lz,你要实现的单点对于以上情况是什么标准呢??是A能上时,B能上呢,还是只能有一个人能在线呢??我建议采取“只能有一个人能在线”{最初的单点思想,就是保证用户的唯一性,不是吗?},解决方法就是,当A在线时,B如果要上则,B代替A{A将不能以用户身份操作},反之一样。也许你会反对,但我只说一句话“一个账户可能有两个持有都吗?实际中,没人愿意共同持有一个帐户。”
    说到这,如果按我的说法,用Cookie以与Cache就可能了{Cache中保存着最新的用户的ID号和随机数[只有随机数相同才能有权操作系统。]}希望对LZ有帮助。