Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

@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. I put breakpoint for looking process but it get UserLog action doesn't UserOut. My question is that where I use wrong way of cookie? What is a best way using cookie in Asp.Net Mvc4 for this scenario ?

like image 934
Elvin Mammadov Avatar asked Oct 02 '13 01:10

Elvin Mammadov


People also ask

How cookies works in ASP NET MVC?

In ASP.Net MVC application, a Cookie is created by sending the Cookie to Browser through Response collection (Response. Cookies) while the Cookie is accessed (read) from the Browser using the Request collection (Request. Cookies).

Does ASP Net use cookies?

By default, ASP.NET uses a non-persistent cookie to store the session state. However, if a user has disabled cookies on the browser, session state information cannot be stored in a cookie.

How do I access cookies in asp net?

You may use Request. Cookies collection to read the cookies. Show activity on this post. HttpContext.


2 Answers

Try using Response.SetCookie(), because Response.Cookies.Add() can cause multiple cookies to be added, whereas SetCookie will update an existing cookie.

like image 167
GvM Avatar answered Oct 07 '22 08:10

GvM


We are using Response.SetCookie() for update the old one cookies and Response.Cookies.Add() are use to add the new cookies. Here below code CompanyId is update in old 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. 
like image 44
Anil Singh Avatar answered Oct 07 '22 08:10

Anil Singh