饼干。为什么惯于这项工作? [英] Cookies. Why wont this work?

查看:131
本文介绍了饼干。为什么惯于这项工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我失去了,现在最简单的事情,不会为我工作。

好吧,如果我运行此脚本,只有第一步 - 它将返回NOTNULL - 好!

如果我然后运行它由于只有第2步它会为预期收益率空 - 好!

但是,如果我再删除这两个步骤1和2(删除与步骤2的饼干后)
它切换回NOTNULL! - BAAD

怎么可能呢?如何检查,如果cookie是空!?

希望你能帮助:)

  //第1步
        HttpContext.Response.Cookies.Add(新的HttpCookie(测试){名称=测试,过期= DateTime.Now.AddDays(2)});        //干2
        。HttpContext.Request.Cookies [测试] =到期DateTime.Now.AddDays(-2);
        HttpContext.Request.Cookies.Remove(测试);
        HttpContext.Request.Cookies.Clear();
        字符串测试=空;        如果(HttpContext.Request.Cookies [测试]!= NULL)
        {
            测试=NOTNULL;
        }        返回含量(试验);


解决方案

在第二个步骤中,您要修改Cookie,但你仍然请求对象的工作。要提交你的修改需要响应对象。于是,经过你的第2步的cookie仍然活着。

  //干2
    。HttpContext.Response.Cookies [测试] =到期DateTime.Now.AddDays(-2);
    HttpContext.Response.Cookies.Remove(测试);
    HttpContext.Response.Cookies.Clear();

i am lost, now the most simple things wont work for me.

Okay, so if i run this script with only Step1 - It will returns "NotNull" - good!

If i then run it With only Step 2 it would as expected return "Null" - good!

But if i then remove Both Step 1 AND 2 (After removing the cookie with step2) It switches back to "NotNull" - Baad!

How can that be? How can i check if a cookie is Null !?

Hope you can help :)

        //Step 1
        HttpContext.Response.Cookies.Add(new HttpCookie("test") { Name = "test", Expires = DateTime.Now.AddDays(2) });

        //Stem 2
        HttpContext.Request.Cookies["test"].Expires = DateTime.Now.AddDays(-2);
        HttpContext.Request.Cookies.Remove("test");
        HttpContext.Request.Cookies.Clear();


        string test = "Null";

        if (HttpContext.Request.Cookies["test"] != null)
        {
            test = "NotNull";
        }

        return Content(test);

解决方案

In the second step you are trying to modify cookies but you still working with Request object. To submit your modifications you need Response object. So, after your 2nd step cookies still alive.

    //Stem 2
    HttpContext.Response.Cookies["test"].Expires = DateTime.Now.AddDays(-2);
    HttpContext.Response.Cookies.Remove("test");
    HttpContext.Response.Cookies.Clear();

这篇关于饼干。为什么惯于这项工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆