How to redirect on ASP.Net Core Razor Pages

2019-01-28 07:33发布

问题:

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?

回答1:

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 that code in production.



回答2:

Same for pages without cs:

@page

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

        return Redirect(url);
    }
}