C#自动登录问题请大家帮忙

贾志轩 2009-04-29 04:21:48
如题,我想作自动登录,请高手们帮忙,写的越详细越好,谢谢大家(其实就是怎么保存cookies,再次访问时该怎么读取)
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
麻子Mozart 2009-04-29
  • 打赏
  • 举报
回复
帮顶
guichuan 2009-04-29
  • 打赏
  • 举报
回复
看看我空间 上面有说
蓝海D鱼 2009-04-29
  • 打赏
  • 举报
回复
mark
salior2006 2009-04-29
  • 打赏
  • 举报
回复
UP
老郭20130824 2009-04-29
  • 打赏
  • 举报
回复
给你一段代码

login.aspx.cs

string userId = 111;
Session["userId"] = userId;
WriteCookie("__MyCookieName", "__MyCookieKey", userId.ToString(), DateTime.Now.AddDays(1));
Response.Redirect("xxx.aspx");


xxx.aspx.cs
string userId;
object o = Session["userId"];
if(o != null)
userId = (int)o;
else
{
string sUserId = ReadCookie("__MyCookieName","__MyCookieKey");
if(string.IsNullOrEmpty(sUserId))
throw new Exception("login timeout");
else
userId = int.parse(sUserId);
}
Response.Write(userId);



2函数
public string ReadCookie(string cookieName, string cookieKey)
{
try
{
HttpCookie _cookie = Request.Cookies[cookieName];
if (_cookie == null)
return "";
return _cookie[cookieKey];
}
catch { return ""; }
}

public void WriteCookie(string cookieName, string cookieKey, string cookieValue, DateTime expire)
{
try
{
HttpCookie _cookie = HttpContext.Current.Request.Cookies[cookieName];
if (_cookie == null)
_cookie = new HttpCookie(cookieName);
_cookie[cookieKey] = cookieValue;
_cookie.Expires = expire;
HttpContext.Current.Response.AppendCookie(_cookie);
}
catch { }
}



随手写的,错误自己修改

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧