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就正常,这是为什么? 
        }
    }

解决方案 »

  1.   

    IsNumberic(name) name是"xid",这里不是数字啊
      

  2.   

      HttpContext.Current.Response.Write("xid=" + MyCookie.GetCookie("xid").ToString());
                HttpContext.Current.Response.Write("xname=" + MyCookie.GetCookie("xname").ToString());
      

  3.   

    if ((cookie != null) && (IsNumberic(name)))
    這個可能嗎?
    xid和xname都是false
      

  4.   

    大家帮帮我吧,我真的晕的什么都不会了,我就想实现以下功能,请朋友们帮我写一下代码,感谢!要两个cookie,一个存id,一个存用户名然后再其它类里分别读取(全局变量)
      

  5.   

    现在能读取了,但是xid一会就不存在了,只有xname还存在,是为什么?
                MyCookie.SetCookie("xid", "100", 30);
                MyCookie.SetCookie("xname", "长空", 30);    
      

  6.   

    测试了好久,发现是aspx读不到cookie,而用js到是能读到,我把aspx中原来需要用到cookie值的地方都改成实际值都OK,现在的问题就是cookie不能被aspx读到,这是什么原因呢?
    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?)
      

  7.   

    同2楼 MyCookie.GetCookie("xid");
    调用错方法了。。MyCookie.GetCookieId("xid");