using System;
using System.Web;
namespace MySecurity
{
public class MyCookie
{
public static void DeleteCookie(string cookieName)
{ HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie != null)
{
cookie.Expires = DateTime.Now;
HttpContext.Current.Response.Cookies.Add(cookie);
}
} public static string GetCookie(string name)
{ HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
if (cookie != null)
return cookie.Value;
else
return null;
} public static int GetCookieId(string name)
{ HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
if ((cookie != null) && (IsNumberic(name)))
return Convert.ToInt32(cookie.Value);
else
return 0;
} public static void SetCookie(string name, string value, int minutes)
{ HttpCookie cookie = new HttpCookie(name, value);
cookie.Expires = DateTime.Now.AddMinutes(minutes);
HttpContext.Current.Response.Cookies.Add(cookie);
} /**//// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
public static bool IsNumberic(string oText)
{
try
{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
{
return false;
}
} }
}public class Login:System.Web.UI.Page
{
public Login()
{
MyCookie.SetCookie("xid", "100", 30);
MyCookie.SetCookie("xname", "长空", 30);
}
}
public class View:System.Web.UI.Page
{
public View()
{
int dealerid = MyCookie.GetCookieId("xid");
HttpContext.Current.Response.Write("xid=" + MyCookie.GetCookieId("xid").ToString());
xid显示为空,xname就正常,这是为什么?
}
}
解决方案 »
- 有人知道怎么在页面加载的时候初始化DETAILVIEW的EditItemTemplate里面的控件吗?
- 匹配日期时间的正则表达式
- 错误 1 “lstRole”不是“admin_addUser”的成员。 E:\WebSite\admin\addUser.aspx.vb 10 34 http://localhost/srms/
- 如何读取应用中子目录中的文件内容
- post 的小问题
- 求简单的一个sql语句...帮忙吧...
- 在Panel控件中怎么动态添加一个换行符?
- 将xml读到dataset,有 4 张表, 该怎么把这 4 张表的各个列 绑定到DataGrid控件上呢?
- 怎么样才能提高自己的.NET水平呢,自己做例子吗?
- xslt中数据移动问题
- 客户端用ie6访问excel的时候老师无法建立连接
- UpdatePanel中div显示的问题
HttpContext.Current.Response.Write("xname=" + MyCookie.GetCookie("xname").ToString());
這個可能嗎?
xid和xname都是false
MyCookie.SetCookie("xid", "100", 30);
MyCookie.SetCookie("xname", "长空", 30);
web.config内容
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<customErrors mode="Off"/>
<authentication mode="None"/>
<globalization fileEncoding="gb2312" requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" uiCulture="zh-CHS"/>
</system.web>
</configuration>
public static void Write2Cookie(string uid,string uname,int longtime)
{
HttpCookie cookie = new HttpCookie("userInfo");
cookie.Values["uid"] = HttpUtility.UrlEncode(uid);
cookie.Values["uname"] = HttpUtility.UrlEncode(uname);
cookie.Expires = DateTime.Now.AddMinutes(longtime);
HttpContext.Current.Response.Cookies.Add(cookie);
} public static string Read2Cookie(string cookiefathername,string cookiesunname)
{
if (HttpContext.Current.Request.Cookies[cookiefathername] != null)
{
return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookiefathername][cookiesunname]);
}
else
{
return "";
}
}以下是js,运行在htm文件中 function get2Cookie(name) {
var r = new RegExp("(^|;|\s)*" + name + "=([^;]*)(;|$)");
var m = document.cookie.match(r);
return (!m ? null : decodeURIComponent(m[2]));
}
目前正的是晕了,各位老大帮看看什么原因(怎么就读不到cookie?)
调用错方法了。。MyCookie.GetCookieId("xid");