单点登录

来源:互联网 发布:数据库设置主键自增 编辑:程序博客网 时间:2024/04/27 19:14
    if (i > 0)
        {
            // 作为唯一标识的str_Key,应该是唯一的,这可根据需要自己设定规则。
            // 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。
            // 生成str_Key
            string str_Key = username;//你可以用用户名作为标识.
            // 得到Cache中的给定str_Key的值
            string str_User = Convert.ToString(Cache[str_Key]);
            // Cache中没有该str_Key的项目,表示用户没有登录,或者已经登录超时
            if (str_User == String.Empty)
            {
                // TimeSpan构造函数,用来重载版本的方法,进行判断是否登录。
                TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
                HttpContext.Current.Cache.Insert(str_Key, str_Key, null, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, null);
                Session["User"] = str_Key;
                // 首次登录成功
                Response.Write("<script>window.alert('您登录成功!')</script>");            }
            else
            {
                // 在 Cache 中存在该用户的记录,表名已经登录过,禁止再次登录
               Response.Write("<script>window.alert('您已经登录!')</script>");
            }

        }
        else
        {
            Response.Write("<script>window.alert('用户名称或密码错误!!!')</script>");
        }