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延时后这个会不会跟着延时呢
谢谢
{ }
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延时后这个会不会跟着延时呢
谢谢
如果用户有这个登录的Cookie的话可以登录
如果没有的话检查Cache看是否有这个用户的Cache
如果有的话说明已经登录,否则还没有登录但这里有一个问题是如何来设计Cache和Cookie的过期问题
我是设计和Session一样的时间20分钟
但Session每当有用到都会自动延时,而Cookie和Cache这时还是那个时间
所以过了20分钟的话 这个单点登录就不成立了 因为Cooki和Cache都消失了
说到这,如果按我的说法,用Cookie以与Cache就可能了{Cache中保存着最新的用户的ID号和随机数[只有随机数相同才能有权操作系统。]}希望对LZ有帮助。