Cookie在Jquery帖子中返回null到MVC Core2控制器

时间:2018-03-13 00:02:11

标签: asp.net-mvc cookies

我的cookie代码返回null,我正在尝试匿名用户活动并登录到文件

这是我的代码 和cookie设置为

  public IActionResult Index(string key, string value, int? expireTime)
    {
        key = "localhost";
        value = "Upload_Page";
        expireTime = 10000;
        CookieOptions option = new CookieOptions();

        if (expireTime.HasValue)
            option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
        else
            option.Expires = DateTime.Now.AddMilliseconds(10);

        Response.Cookies.Append(key, value, option);

        return View();
    }

 string key; string value; int? expireTime;



            key = Request.Cookies["key"];
            value = Request.Cookies["value"];
            expireTime = Int32.Parse(Request.Cookies["expireTime"]);

1 个答案:

答案 0 :(得分:0)

你没有提供你的获取逻辑。确保您通过回复发送cookie并通过请求获取cookie,如下所示:

    public IActionResult CreateCookie()
    {

        const string key = "localhost";
        const string value = "Upload_Page";
        var option = new CookieOptions { Expires = DateTime.Now.AddYears(10) 
    };

        Response.Cookies.Append(key,value,option);

        return View();
    }

    public IActionResult LoadCookie()
    {
        var userCookie = Request.Cookies["localhost"];
        return View();
    }