简体   繁体   English

在 Asp.Net Mvc 4 中使用 Cookie

[英]Using Cookie in Asp.Net Mvc 4

I have web application in Asp.Net MVC4 and I want to use cookie for user's login and logout.我在Asp.Net MVC4 中有 Web 应用程序,我想使用cookie进行用户登录和注销。 So my actions as follows:所以我的操作如下:

Login Action登录操作

    [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");
    }

LogOut Action注销操作

    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");
    }

And I use this cookie in _Loyout as follow:我在 _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>
  }

But When I click *UserOut* action this action happen first time, but then it doesn't work.但是当我单击*UserOut* 操作时,此操作第一次发生,但随后不起作用。 I put breakpoint for looking process but it get UserLog action doesn't UserOut .我为查找过程设置了断点,但它得到UserLog操作不是UserOut My question is that where I use wrong way of cookie?我的问题是我在哪里使用了错误的 cookie 方式? What is a best way using cookie in Asp.Net Mvc4 for this scenario ?对于这种情况,在Asp.Net Mvc4 中使用 cookie 的最佳方法是什么?

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

We are using Response.SetCookie() for update the old one cookies and Response.Cookies.Add() are use to add the new cookies.我们使用Response.SetCookie()来更新旧的 cookie,使用Response.Cookies.Add()来添加新的 cookie。 Here below code CompanyId is update in old cookie[OldCookieName] .下面的代码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.
userCookie.Expires.AddDays(365); 

This line of code doesn't do anything.这行代码没有做任何事情。 It is the equivalent of:它相当于:

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

You probably want你可能想要

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM