When to use LocalRedirect vs RedirectToPage

Viewed 12442

With ASP.Net Core 2.1 Razor Pages, what is the best practice on using LocalRedirect() vs. RedirectToPage()?

It seems they can be used interchangeably when redirecting to a page within the current website. Is there an advantage to one over the other?

1 Answers

LocalRedirect should be used when you're dealing with a "return URL", i.e. you're passing around a URL that the user should be redirected back to after some process is complete, such as logging in. In such cases, a malicious actor could send a user to your login form, for example, with a return URL back to a malicious site. Using LocalRedirect ensures that the "return URL" is a route actually on your site, instead of some malicious third-party bad actor's.

All the other redirect result types can be used when you are directly controlling where the user is being redirected to.

Related