Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#偶尔索引的cookie是';t保存并在写入后返回原始值_C#_Cookies - Fatal编程技术网

C#偶尔索引的cookie是';t保存并在写入后返回原始值

C#偶尔索引的cookie是';t保存并在写入后返回原始值,c#,cookies,C#,Cookies,我有一个用C#读写cookies的应用程序。有时,这些cookie由ASP经典页面读取或写入并共享。当classic编写类似于course_int的内容时,它实际上存储为course%5Fint。所以,当我在C#Net中阅读或写作时,我会将所有下划线转换为%5F,并且一切正常。。。大多数时候。有时候,cookie值会被“卡住”,无法更改。。。即使我在.Net中阅读和编写它们,并确保asp classic没有触动它们。我写了一个测试程序来读取cookie,显示,设置一个值,显示。结果是: 54 九

我有一个用C#读写cookies的应用程序。有时,这些cookie由ASP经典页面读取或写入并共享。当classic编写类似于course_int的内容时,它实际上存储为course%5Fint。所以,当我在C#Net中阅读或写作时,我会将所有下划线转换为%5F,并且一切正常。。。大多数时候。有时候,cookie值会被“卡住”,无法更改。。。即使我在.Net中阅读和编写它们,并确保asp classic没有触动它们。我写了一个测试程序来读取cookie,显示,设置一个值,显示。结果是:

54 九十九

但是,我第二次运行它时,除了刷新页面外,什么也不做,得到了完全相同的输出。。。就像在调试器中从未编写过一样,我可以看到它是。代码如下所示:

在.aspx中

        <%=course_int %><br />
        <hr />
        <%=course_int1 %><br />
当我查看实际cookie.Value时,它是:

ReturnXT=&site%5Fname=&thm=131&LMW=1&btnFA=%2377AAD7&btnC=%232E7CC1&hovC=%234C619C&pagF=%2320252b&bill%265Fdate=&lesson%265Fint=&test%5Fint=37&DOG=1&MP=1&jcslimited=&company=ALL&mnuF=%23000000&part=&0=1&lesson%5Fint=0&lesson%5Fint=&lesson%0&site%5Fparam=&ADM=0&PAI=1&DT=1&org%5Fint=1&org%5Fint=1&deocon=1&mdash=1&mdash&mdash&mdash=1&mdash&mdash=0&mdash=0&mdash=0&mderm=1%2C2%2C3%2C4%2C6%2C10&btnF=%23ffffff&HOFF=%23ffffff&PII=1&test%265Fint=&ReturnX=https%3A%2F%2HOBART%2learn%2Enet%2Flmw%2F&less%5Fname=&cld=&hmLoc=https%3A%2F%2HOBART%2learn%2Enet%3A443%2EFAULT%2ASP&limit%5Fparam=&pcode=No+Department&dept=&person%5Fid=stever&person%5Fname=&TT3=265FFFFOF=23FDEAM=2651%=&NB%5F3%5F54=&课程%5Fname=血源性病原3PS4Elessoncopy

&课程%5Fint=99&dhm=C%3A%5Cinetpub%5C&ED=&person%5Fname=Steve+Ricketts&h1=%2320252b&PCI=1 &课程%265Fint=&问题%265Fint=&订单%5Fdate=&账单%5Fdate=&问题%5Fint=&指南%5Fseq=0&根=%2Flmw%2F

&EH=&人员%5Fkey=stever&G2T=&myTM=0&mnuC=% 23efefef&DS=video%2EFvideoselection%2ASP%3FDT%3D1%26SM%3DASGN%26bUn%3D1%26bLng%3D1&schedule%265Fdate=&schedule%5Fdate=

很明显,我在球场上把它们分开了,这样你可以更容易地看到。奇怪的是,这里也有一个&course%265Fint。。。不知道这是怎么回事。我尝试过删除整个cookie,设置cookie,然后添加它。我能想到的所有组合


所以,我肯定我错过了什么。。。它写的是99,但下次读的时候,还是54。这是服务器2019年,但也发生在2008年和2016年。我做错了什么以及如何修复它?

原来我在cookie中得到了另一个域,所以我有一个路径/和一个路径/lmw,这两个路径的值相似。显然,我可以改变一个,但不能改变另一个。如果不删除所有cookie,我就无法删除/lmw域,因此我对一个页面进行了AJAX调用,该页面刚刚清除了/lmw域cookie,并且成功了。我又能读/写饼干了

    course_int = getCookieValue("course_int");
    setCookieValue("course_int", "99");
    course_int1 = getCookieValue("course_int");

    public static string getCookieValue(string CookieName)
    {
        string CookieValue = "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies["LS"];
            CookieName = CookieName.Replace("_", "%5F");
            if (cookie.Values[CookieName] != null)
            {
                CookieValue = cookie.Values[CookieName];
            }
        }
        return CookieValue;


    public static void setCookieValue(string CookieName, string CookieValue)
    {
        HttpCookie cookie;
        string NetCookieName = CookieName.Replace("_", "%5F");
        cookie = HttpContext.Current.Request.Cookies["LS"];

        if ( CookieValue == "")
        {
            cookie.Values.Remove(NetCookieName);
        }
        else
        {
            if (cookie.Values[NetCookieName] == null)
            {
                cookie.Values.Add(NetCookieName, CookieValue);
            }
            else
            {
                cookie.Values.Set(NetCookieName, CookieValue);
            }
        }
        HttpContext.Current.Response.Cookies.Set(cookie);
        HttpContext.Current.Request.Cookies.Set(cookie); (a post suggested this too)
        (also tried deleting and adding, no difference)