Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect on ASP.Net Core Razor Pages

I am using the new Razor Pages in ASP.Net core 2
Now I need to redirect

I tried this, but the page does not redirect:

public class IndexModel : PageModel {     public void OnGet()     {         string url = "/.auth/login/aad?post_login_redirect_url=" + Request.Query["redirect_url"];          Redirect(url);     } } 

How to redirect?

like image 468
Tony Avatar asked May 10 '18 21:05

Tony


People also ask

How do I redirect in razor page?

You can use the IActionResult to return a redirection or your razor page.

How we can redirect to another page or controller?

Redirect() method The first method od redirecting from one URL to another is Redirect(). The Rediect() method is available to your controller from the ControllerBase class. It accepts a target URL where you would like to go.


1 Answers

You were very close. These methods need to return an IActionResult (or Task<IActionResult> for async methods) and then you need to return the redirect.

public IActionResult OnGet() {     string url = "/.auth/login/aad?post_login_redirect_url="        + Request.Query["redirect_url"];      return Redirect(url); } 

Razor pages documentation

However, you have a huge Open Redirect Attack because you aren't validating the redirect_url variable. Don't use this code in production.

like image 152
Erik Philips Avatar answered Sep 29 '22 15:09

Erik Philips