Cookie未创建

时间:2014-05-05 09:39:41

标签: c# cookies

您好,如果有以下代码:

protected void btnAdmLogin_Click(object sender, EventArgs e)
      {
        DBDataContext dc = new DBDataContext();

        try
        {
            PortalUser login = dc.PortalUsers.Single(q => q.UserName.Equals(txtAdmLoginEmail.Text) && q.Password.Equals(txtAdmLoginPassw.Text));

            if (HttpContext.Current.Request.Cookies["Cookie"] == null)
            {
                HttpCookie adcookie = new HttpCookie("Cookie");
                adcookie.Expires = DateTime.Now.AddHours(24);
                adcookie.Value = login.UserName + "-" + 1;
                adcookie.Name = "Cookie";
                HttpContext.Current.Request.Cookies.Add(adcookie);
            }

                HttpContext.Current.Response.Redirect("Overview");

        }

错误的部分在这里?它没有创造任何东西。

1 个答案:

答案 0 :(得分:0)

您应该将cookie添加到Response对象而不是Request对象

  HttpContext.Current.Response.Cookies.Add(adcookie);