Boris Callens
Boris Callens

Reputation: 93327

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?

Upvotes: 10

Views: 3175

Answers (1)

bzlm
bzlm

Reputation: 9727

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.

Upvotes: 3

Related Questions