Open redirect

Open redirects in C#

Description

Using a return URL from the request can redirect users outside the application.

Potential impact

A trusted link can lead to a phishing page. An authentication flow that trusts the return URL may also disclose tokens.

Remediation

Allow only local paths for navigation within the application. If users need to leave the site, select from destinations registered on the server.

Examples

The first excerpt uses the Minimal API method Results.Redirect. In the second, Url, Redirect and LocalRedirect belong to an MVC controller.

Before

csharp
return Results.Redirect(returnUrl);

After

csharp
if (!Url.IsLocalUrl(returnUrl)) {
    return Redirect("/dashboard");
}
return LocalRedirect(returnUrl);

The MVC controller passes a path to LocalRedirect only if Url.IsLocalUrl accepts it. Other values lead to /dashboard.

References