注册 登录
编程论坛 ASP.NET技术论坛

求助: asp.net 中cookies 无法保存

notlook 发布于 2010-06-30 14:01, 3957 次点击
最近想实现 网页登陆成功---关掉网页---然后再打开网页就直接实现登陆的效果   但是一直无法成功


希望各位高手帮我  做一个小案例 参考一下


在这里先谢谢各位了!
29 回复
#2
冰镇柠檬汁儿2010-06-30 14:45
用以下的方法写入
Response.Cookies["u_name"].Value = Server.UrlEncode(this.txtUserName.Text);
Response.Cookies["u_name"].Expires = DateTime.Now.AddDays(365);
用下面的代码读取
Server.UrlDecode(Request.Cookies["u_name"].ToString());

其中Server.UrlEncode和Server.UrlDecode是在需要写入中文的时候加,是对其进行编码和解码的,避免出现乱码,一般情况下用了Server.UrlEncode编码后,不需要解码就可以直接用了
#3
notlook2010-06-30 15:28
回复 2楼 冰镇柠檬汁儿
还是不行啊,能不能写个附件发上来 我看看那到底是代码原因还是电脑设置原因!
先谢谢了!
#4
notlook2010-07-01 06:50
只有本站会员才能查看附件,请 登录


大家帮我看看什么问题啊,怎么就是弄不好呢?
#5
冰镇柠檬汁儿2010-07-01 12:51
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Response.Cookies["name"].Value != null)
            {
                Server.Transfer("Default2.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Response.Cookies["name"].Value = TextBox1.Text;
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(1);
            Server.Transfer("Default2.aspx");
        }
    }
}
看看你的代码,Page_Load里if (Response.Cookies["name"].Value != null),你还没点按钮的时候,还没写Cookies的时候你就读,能读到什么呢?
#6
notlook2010-07-01 14:05
回复 5楼 冰镇柠檬汁儿
对啊,是读不到cookies .  不过我的意思是说:1.打开网站  2.如果读不到cookies就打开网页a,然后输入信息并保存cookies(就是button1里面的代码)然后跳到网页b(在网页b中是可以读取到cookies的)   3.关掉网页b  4.再次打开网站(就是执行 Page_Load 里面的代码)看看是否有读取到cookies-------不过都是读取不到cookies

ps:我不知道我的想法是不是正确,我只是想实现就犹如本论坛:登陆成功后,关掉网页,再次打开网页还是保存登陆状态的功能.
#7
冰镇柠檬汁儿2010-07-01 14:47
可是Page_Load里的Response.Cookies["name"].Value会直接报错,因为Response.Cookies["name"]的值是null,那么其value就是不存在的,明白我的意思吗?
#8
notlook2010-07-01 15:30
回复 7楼 冰镇柠檬汁儿
这个我明白的! 可能是我的想法有问题的.  我只是想实现那个功能而已啊,而我只能想到这种实现的形式,因为网上说用cookies实现的.....而我有无法实现,  所以我才发帖子想要一个实现我功能的附件,看看到底是怎么实现的啊!!!

希望帮帮忙 ,麻烦一下
#9
冰镇柠檬汁儿2010-07-01 23:38
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Response.Cookies["name"] != null)
            {
                Server.Transfer("Default2.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Response.Cookies["name"].Value = TextBox1.Text;
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(1);
            Server.Transfer("Default2.aspx");
        }
    }
}


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Response.Cookies["name"] != null && Response.Cookies["name"].Value != "")
        {
            Label1.Text = Response.Cookies["name"].Value;
        }
    }
}

这样,基本的功能就应该可以了
#10
notlook2010-07-02 08:46
回复 9楼 冰镇柠檬汁儿
好像还是不行啊!   第一次运行的时候  跳出的Default.aspx页面  显示不出来 TextBox 和Button 啊.....
#11
冰镇柠檬汁儿2010-07-02 09:10
我之前理解有误,楼主抱歉了,我改了下代码,代码如下:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Response.Cookies["name"].Value != null)
            {
                Response.Redirect("Default2.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Response.Cookies["name"].Value = TextBox1.Text;
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(1);
            Response.Redirect("Default2.aspx");
        }
    }
}

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Request.Cookies["name"].Value;
    }
}

请注意我标红的行的写法

[ 本帖最后由 冰镇柠檬汁儿 于 2010-7-2 09:11 编辑 ]
#12
冰镇柠檬汁儿2010-07-02 09:13
我想谢谢楼主,让我又重新复习了下Cookies
#13
notlook2010-07-02 10:17
回复 11楼 冰镇柠檬汁儿
不知道怎么的还是有问题啊,还是实现不了啊! 版主能不能发一个测试成功的附件给我啊 !!!!!
#14
notlook2010-07-02 10:18
回复 12楼 冰镇柠檬汁儿
是我要谢谢你啊,这么辛苦的帮我解决问题!!!
#15
冰镇柠檬汁儿2010-07-02 10:41
回复 13楼 notlook
我在11楼写的代码,就是我测试成功后的,html代码我没动的,怎么还会有问题呢?
#16
bygg2010-07-02 10:42
依我看,让LZ将对应的代码发出来吧,这样大家好定位.
#17
notlook2010-07-02 11:23
回复 16楼 bygg
其实就是上面说的这几句代码啊!!!
只有本站会员才能查看附件,请 登录
#18
bygg2010-07-02 12:14
if (Response.Cookies["name"].Value != null)

===>

if (!string.IsNullOrEmpty(Response.Cookies["text"].Value))
#19
冰镇柠檬汁儿2010-07-02 12:43
以下是引用bygg在2010-7-2 12:14:18的发言:

if (Response.Cookies["name"].Value != null)

===>

if (!string.IsNullOrEmpty(Response.Cookies["text"].Value))
我测试过,Response.Cookies["name"].Value确实是有null值,作为简单的测试,这个我决定可以
#20
notlook2010-07-02 14:06
.....还不不行啊   现在有两种情况
 1.if (Response.Cookies["name"].Value != null)
跟以前一样  关掉后在打开出现页面1  


2.if (!string.IsNullOrEmpty(Response.Cookies["text"].Value))
关掉后打开出现页面2 但是出现错误(未将对象引用设置到对象的实例。)
#21
bygg2010-07-02 14:51
我知道为空,但是Response.Cookies["name"].Value != null确是true.所以用我那个方法可以判断.
#22
bygg2010-07-02 15:06
我这边是可以的.如果还是不行,就将保存那段代码换成另一种方式:
程序代码:


            HttpCookie aCookie = new HttpCookie("text");

            aCookie.Value = TextBox1.Text.Trim();

            aCookie.Expires = DateTime.Now.AddDays(10);

            Response.Cookies.Add(aCookie);
if (!string.IsNullOrEmpty(Response.Cookies["text"].Value))
出现null的话,就在外面再加一个
if (Response.Cookies["text"] != null)

另外,确认你的IE是开启了支持Cookie的.

[ 本帖最后由 bygg 于 2010-7-2 15:10 编辑 ]
#23
冰镇柠檬汁儿2010-07-02 16:09
以下是引用bygg在2010-7-2 14:51:28的发言:

我知道为空,但是Response.Cookies["name"].Value != null确是true.所以用我那个方法可以判断.
为true,就跳转啊,不为空就到页面2中显示,我觉得应该没问题吧
#24
a544009502010-07-02 17:28
哈哈
这个我也懂
#25
saitor2010-07-02 20:10
请检查是否是禁用了COOKIE。
#26
bygg2010-07-02 21:04
以下是引用冰镇柠檬汁儿在2010-7-2 16:09:47的发言:

为true,就跳转啊,不为空就到页面2中显示,我觉得应该没问题吧
Response.Cookies["name"].Value = null
然而Response.Cookies["name"].Value != null的值却是true.
至少我这里是这样的.
#27
notlook2010-07-03 09:18
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["name"].Value != null)
            {
                Server.Transfer("Default2.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Response.Cookies["name"].Value = TextBox1.Text;
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(1);
            Server.Transfer("Default2.aspx");
        }
    }
}

这样子就好了

[ 本帖最后由 notlook 于 2010-7-3 10:54 编辑 ]
#28
冰镇柠檬汁儿2010-07-03 15:25
居然是这种问题
#29
bygg2010-07-03 19:06
晕,现在才发现。哈哈,冰镇柠檬汁儿,我们一起撞墙去。
#30
冰镇柠檬汁儿2010-07-04 20:36
别,我可不去,我发的代码里已经写了,并且标红了,没人注意而已
1