问题:
使用Response.Cookies["nameID"].Value="007";来创建cookie对象,直接运行其他页面无法取得Value,并且用户端的cookie目录也没有cookie文件,怎样处理? 怎样才能正常生成cookie文件.

解决方案 »

  1.   

    DateTime dt=DateTime.Now; //得到当前时间
    HttpCookie mycookie=new HttpCookie("logname");//申明新的COOKIE变量
    mycookie.Value="aa";//赋值
    mycookie.Expires=Convert.ToDateTime(dt+TimeSpan.FromDays(1));//设定过期时间为1天
    Response.Cookies.Add(mycookie1);//写入COOKIE
      

  2.   

    if (HttpContext.Current.Request.Cookies["userid"] != null )
                {
                    HttpCookie Cookie = HttpContext.Current.Request.Cookies["Userid"];
                    Cookie.Value = ds.Tables[0].Rows[0]["id"].ToString();            }
                else
                {
                    HttpContext.Current.Response.Cookies["userid"].Value = ds.Tables[0].Rows[0]["id"].ToString();
                }这是单个cookies的.
    下面是cookies集合,把127.0.0.1/bbs/改成cookies的名字就行了,我下面的代码是与动网结合写cookies的代码...顺便留留的需要结合动网的人,动网的cookies名必须是论坛所有地址   if (HttpContext.Current.Request.Cookies["127.0.0.1/bbs/"] != null)
                {
                    HttpCookie bbscookie = HttpContext.Current.Request.Cookies["127.0.0.1/bbs/"];
                    bbscookie.Path = "/";
                    bbscookie.Values["username"] = ds.Tables[0].Rows[0]["UserName"].ToString();
                    bbscookie.Values["password"] = ds.Tables[0].Rows[0]["truepassword"].ToString();
                    bbscookie.Values["userclass"] = ds.Tables[0].Rows[0]["userclass"].ToString();
                    bbscookie.Values["userid"] = ds.Tables[0].Rows[0]["userid"].ToString();
                    bbscookie.Values["userhidden"] = "2";
                    bbscookie.Values["usercookies"] = "0";
                    bbscookie.Values["StatUserID"] = DateTime.Now.ToString();
                    bbscookie.Expires = DateTime.Now.AddDays(1);
                    HttpContext.Current.Response.AppendCookie(bbscookie);
                }
                else
                {
                    HttpCookie bbscookie = new HttpCookie("127.0.0.1/haolives/bbs/");
                    bbscookie.Path = "/";
                    bbscookie.Values["username"] = ds.Tables[0].Rows[0]["UserName"].ToString();
                    bbscookie.Values["password"] = ds.Tables[0].Rows[0]["truepassword"].ToString();
                    bbscookie.Values["userclass"] = ds.Tables[0].Rows[0]["userclass"].ToString();
                    bbscookie.Values["userid"] = ds.Tables[0].Rows[0]["userid"].ToString();
                    bbscookie.Values["userhidden"] = "2";
                    bbscookie.Values["usercookies"] = "0";
                    bbscookie.Values["StatUserID"] = DateTime.Now.ToString();
                    bbscookie.Expires = DateTime.Now.AddDays(1);
                    HttpContext.Current.Response.AppendCookie(bbscookie);
                
                }
      

  3.   

    没搞懂你,在网上查查代码就能搞定的问题,你却来发问.这不要紧,你竟然对我说:"麻烦楼主自己干点正事好不好,这是技术区,你感觉不好的话首先可以不用它开发,
    另外也可以投诉微软公司,说风凉话有啥用."
    多学习一下用baidu .有些问题不必发问也能解决..你不过是养成一种喜欢麻烦别人的性格..
      

  4.   

    /// <summary>
            /// 读取Cookies--UserID
            /// </summary>
            /// <returns></returns>
            public static int CookUserID()
            {
                int UserId=0;
                if (HttpContext.Current.Request.Cookies["userid"] != null)
                {
                    UserId = Convert.ToInt16(HttpContext.Current.Request.Cookies["userid"].Value);
                }
                else
                {
                    HttpContext.Current.Response.Redirect("Login.aspx");
                }
                return UserId;
            }
      

  5.   

    多学习一下用baidu .有些问题不必发问也能解决..你不过是养成一种喜欢麻烦别人的性格..
    ---------------------------------------------------------------------------------
    在高手认为是很简单的问题,菜鸟也可能会不理解,尽管他已经做了很努力的思考,
    老实讲我到现在都没有完全看懂上面的大段代码,但已经足够了,因为有了足够多的提示..若干年后我想我也会偶尔回答一下菜鸟的问题,因为高手先驱们已经做出了表率,
    新手的问题总会被回答, 技术社区才会昌盛和温馨, 甚至能够使更多的人加入进来,这样
    整个业界才能够强大起来.被你看不起的 .net才有机会做出更大更多更强更快的项目.