Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access Request.cookies in an ASP.NET MVC controller?

Tags:

c#

asp.net-mvc

I'm trying to get a user ID stored in cookies via a common Controller file, which I can access throughout the site.

I have created FunctionsController as a controller, with content as follows:

public static int loggedinUser() {     return Convert.ToInt32(  request.Cookies["userid"].Value); } 

I am unable to request any cookie items even if I tried with:

HttpRequestBase request = controllerContext.HttpContext.Request; 
like image 834
ANIL MANE Avatar asked Apr 04 '09 06:04

ANIL MANE


People also ask

How do I access cookies in asp net?

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

How does MVC application get the cookies from client?

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.

What is cookies in ASP NET MVC?

Cookies are small files that are created in the web browser's memory (if they're temporary) or on the client's hard drive (if they're permanent). CookiesExample.zip. Cookies are one of the State Management techniques, so that we can store information for later use.

Where are cookies stored asp net?

Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path. Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines.


2 Answers

I don't have a problem accessing cookies in ASP.NET MVC using a standard access statement such as:

Request.Cookies["someCookie"] 

Your sample had a lower-cased "r" in "request.Cookies". Could that be your problem?

like image 51
Ian Suttle Avatar answered Sep 23 '22 12:09

Ian Suttle


Remove the static part of your method declaration and then use Request.Cookies["userId"]

like image 39
ilivewithian Avatar answered Sep 22 '22 12:09

ilivewithian