Korzystanie z plików Cookie w Asp.Net Mvc 4

Mam aplikację webową w Asp.Net MVC4 i chcę użyćcookie do logowania i wylogowania użytkownika. Więc moje działania są następujące:

Akcja Logowania

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

Akcja Wylogowania

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

I używam tego pliku cookie w _Loyout w następujący sposób:

@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>
  }

Ale kiedy Kliknij * userout * akcja ta akcja dzieje się za pierwszym razem, ale potem nie działa. Ustawiłem breakpoint dla szukającego procesu, ale dostaje userlog akcja nie UserOut . Moje pytanie brzmi, gdzie używam niewłaściwy sposób cookie? Jaki jest najlepszy sposób korzystania z plików cookie w Asp.Net Mvc4 dla tego scenariusza ?

Author: Elvin Mammadov, 2013-10-02

3 answers

Spróbuj użyć Response.SetCookie(), ponieważ {[1] } może spowodować dodanie wielu plików cookie, podczas gdy SetCookie zaktualizuje istniejący plik cookie.

 67
Author: GvM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-03 06:04:34

Używamy odpowiedzi.SetCookie() do aktualizacji starego pliku cookie i odpowiedzi.Ciasteczka.Add() służy do dodawania nowych plików cookie. Poniżej kod CompanyId jest aktualizowany w starym pliku 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.
 14
Author: Anil Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-03 06:06:05
userCookie.Expires.AddDays(365); 

Ta linia kodu nic nie robi. Jest odpowiednikiem:

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

Prawdopodobnie chcesz

userCookie.Expires = DateTime.Now.AddDays(365); 
 1
Author: Jonathan Allen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-28 20:47:35