Reputation: 44
I'm trying to redirect from inside a MVC controller to a different external URL, only adding a session cookie that I'd be able to access after redirection.
I've been checking tens of Stackoverflow's questions and more, only found old or irrelevant answers, or with different technologies.
// GET: api/lem/fst?url=xxx
[HttpGet]
public ActionResult Get([FromQuery]string url)
{
var uri = new UriBuilder(url);
var cookieOptions = new CookieOptions
{
Domain = uri.Host,
};
HttpContext.Response.Cookies.Append("key123", "value123", cookieOptions);
return Redirect(uri.ToString());
}
For example, if I send a GET to https://.../api/lem/fst?url=www.google.com, I can see the cookie in the Set-Cookie response header from my server. Also, redirection works fine, while I can't see the cookie anywhere in the request or response from google.
Thanks!
Upvotes: 1
Views: 2051
Reputation: 155
Take a look at: https://stackoverflow.com/a/6761443/7516160
Your code works fine: if it's the same domain then cookie from Get response is stored in cookie inventory and send (in request) to redirected url.
For testing host two seperate web apps on your local machine. For example redirect from localhost:50324 to localhost:47232.
Upvotes: 1