SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In SharePoint 2010 Sandbox solution, whether cookie is supported?

When I'm trying to set cookie by

HttpCookie myCookie = new HttpCookie("Test","Hello"); HttpContext.Current.Response.Cookies.Add(myCookie);

The value is not passed to browser and the Request.Cookies["Test"] is null on next request.

The same code works fine in farm based solution. Please confirm.

share|improve this question
up vote 7 down vote accepted

No you cannot set cookies in the sandbox, the data are not transferred back between the sandbox and the IIS. The Sandbox lives in it's own process and the HttpContext.Current is different from the HttpContext.Current in ASP.NET (that lives in IIS and the w3wp.exe process)

For more limitations see:

share|improve this answer
Thanks. In the case of session, since it shared across web application, the session object is not available in sandbox solution? – Mac Nov 26 '11 at 13:26
No, it has nothing to do with that. Read the linked article - it's the marshalling between processes and the special sandboxed objects that creates this limitation. – Wictor Wilen MCA MCM MVP Nov 26 '11 at 13:44
@Wictor-SharePointMVP,We can read cookie in Sandbox Solutions. – Amit Kumawat Nov 28 '11 at 10:57
@Amit - great, need to update my post on that – Wictor Wilen MCA MCM MVP Nov 28 '11 at 15:37

Regarding cookie support in Sandbox Solutions, let me break it into two parts:

Write a cookie : Not Supported via HTTPContext(Unless you set and use cookie in same sandbox webpart and in same request- which will obviouly not be the case :-))

Read a cookie : You are free to consume cookies in Sandbox Solutions! Assume, you have a cookie which is set already, the below code will work :

string cookieValue= HttpContext.Current.Request.Cookies["Test"].Value;

Another solution which don't need server-side deployment is the JQuery plugin:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.