C#自动登录问题请大家帮忙 如题,我想作自动登录,请高手们帮忙,写的越详细越好,谢谢大家(其实就是怎么保存cookies,再次访问时该怎么读取) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一段代码login.aspx.csstring userId = 111;Session["userId"] = userId;WriteCookie("__MyCookieName", "__MyCookieKey", userId.ToString(), DateTime.Now.AddDays(1));Response.Redirect("xxx.aspx");xxx.aspx.csstring 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 { } }随手写的,错误自己修改 DBHelper.cs C#怎么在没有SQL数据库的电脑上读取SQL数据? 现在有什么最新的技术!!! winform用VS自带的安布署项目如何将.CHM帮助文件加到菜单里面? ConvertDataReaderToDataTable转换时出错,为什么? 请教msmq高手一个问题! 如何调用按钮的Click事件 有关Winform引用WebService的较头疼的问题 一个DataGridView的问题? 请给一个HOOK的例子?比如键盘热键? 怎样把xml文件转换为 byte[] 在线等,急!!! 关于系统助手,求思路!!!
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 { }
}随手写的,错误自己修改