Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.Cookies gets reset when RedirectToAction is called

IN my asp.net-mvc project I have an AccountController that upon logging in sets a cookie with user preferences to the Request.Response and then does a RedirectToAction. Upon redirecting, the cookies are reset so I loose my settings.

The only solution I can come up with is adding the data from the cookie in the tempdata and then fetching it again later in the RedirectToAction's target action. This is off course a little backwards...

Is this a common practice? Is there no better solution? Should I handle my cookies differently?

like image 227
Boris Callens Avatar asked Sep 01 '10 21:09

Boris Callens


1 Answers

Yes, using TempData for this is a common practice, and this is quite in line with how TempData is supposed to be used - passing temporary data between two action methods separated only by a client redirect.

Since the redirect from the login page could be to any other action method, you could implement the functionality to set cookies from TempData in your base controller. This would make any action method cookies-via-TempData compliant. This is a common scenario when displaying notifications on pages, where the notification (like the notifications on this site) would usually travel from TempData to ViewData to the view automatically.

like image 154
bzlm Avatar answered Sep 30 '22 17:09

bzlm