4

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.

2 Answers 2

7

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: http://www.wictorwilen.se/Post/Understanding-the-SharePoint-2010-Sandbox-limitations.aspx

4
  • Thanks. In the case of session, since it shared across web application, the session object is not available in sandbox solution?
    – Mac
    Nov 26, 2011 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. Nov 26, 2011 at 13:44
  • @Wictor-SharePointMVP,We can read cookie in Sandbox Solutions. Nov 28, 2011 at 10:57
  • @Amit - great, need to update my post on that Nov 28, 2011 at 15:37
3

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: http://plugins.jquery.com/project/cookie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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