【问题标题】:Using Cookie in Asp.Net Mvc 4在 Asp.Net Mvc 4 中使用 Cookie
【发布时间】:2013-10-08 08:09:54
【问题描述】:

我在 Asp.Net MVC4 中有 Web 应用程序,我想使用 cookie 进行用户登录和注销。所以我的操作如下:

登录操作

    [HttpPost]
    public ActionResult Login(string username, string pass)
    {
        if (ModelState.IsValid)
        {
            var newUser = _userRepository.GetUserByNameAndPassword(username, pass);
            if (newUser != null)
            {
                var json = JsonConvert.SerializeObject(newUser);

                var userCookie = new HttpCookie("user", json);
                userCookie.Expires.AddDays(365);
                HttpContext.Response.Cookies.Add(userCookie);

                return RedirectToActionPermanent("Index");
            }
        }
        return View("UserLog");
    }

注销操作

    public ActionResult UserOut()
    {
        if (Request.Cookies["user"] != null)
        {
            var user = new HttpCookie("user")
                {
                    Expires = DateTime.Now.AddDays(-1),
                    Value = null
                };
            Response.Cookies.Add(user);
        }
        return RedirectToActionPermanent("UserLog");
    }

我在 _Loyout 中使用这个 cookie,如下所示:

@using EShop.Core
@using Newtonsoft.Json
@{
   var userInCookie = Request.Cookies["user"];
}
...
  @if (userInCookie != null && userInCookie.Value)
  {
        <li><a href="#">Salam</a></li>
        <li><a href="@Url.Action("UserOut", "Home")">Cıxış</a></li>
  }
  else
  {
        <li><a href="@Url.Action("UserLog", "Home")">Giriş</a></li>
  }

但是当我 单击 *UserOut* 操作时,此操作第一次发生,但随后它不起作用。我为查找进程设置了断点,但它得到的 UserLog 操作没有 UserOut。 我的问题是我在哪里使用了错误的 cookie 方式?对于这种情况,在 Asp.Net Mvc4 中使用 cookie 的最佳方法是什么?

【问题讨论】:

  • 您不使用表单身份验证是否有原因?
  • 如何使用它?我知道这一点,但不深入,所以我不能使用表单身份验证。我有一些特殊的过程,我担心表单身份验证无法做到这一点。

标签: c# asp.net-mvc-4 cookies action httpcookie


【解决方案1】:

尝试使用Response.SetCookie(),因为Response.Cookies.Add() 会导致添加多个cookie,而SetCookie 会更新现有的cookie。

【讨论】:

  • 感谢您的回复,但它不再起作用了。请看这个screencast.com/t/eMeFnm6Hq
  • 很奇怪。为什么我的 LogOut Action 没有用断点处理?!但 RedirectToActionParmenently 工作正常 :(
  • 所以这个问题只有在你使用RedirectToAction()时才会出现?
  • 谢谢! Response.SetCookie() 确实有帮助。
【解决方案2】:

我们使用Response.SetCookie() 更新旧cookie,Response.Cookies.Add() 用于添加新cookie。下面的代码CompanyId 更新为旧的cookie[OldCookieName]

HttpCookie cookie = Request.Cookies["OldCookieName"];//Get the existing cookie by cookie name.
cookie.Values["CompanyID"] = Convert.ToString(CompanyId);
Response.SetCookie(cookie); //SetCookie() is used for update the cookie.
Response.Cookies.Add(cookie); //The Cookie.Add() used for Add the cookie.

【讨论】:

    【解决方案3】:
    userCookie.Expires.AddDays(365); 
    

    这行代码没有做任何事情。相当于:

    DateTime temp = userCookie.Expires.AddDays(365); 
    //do nothing with temp
    

    你可能想要

    userCookie.Expires = DateTime.Now.AddDays(365); 
    

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 2012-02-27
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2023-04-11
      • 2012-12-05
      相关资源
      最近更新 更多