C# ASP.NET 4.8 Redirect working properly, but throws a http 403 error in ASP.NET Core 6.0

255 Views Asked by At

I had to write a simple redirection endpoint in ASP.NET 4.8 which works fine, and now I have to rewrite it for ASP.NET Core 6.0.

Redirection endpoint:

[HttpGet]
// GET: Redirect
public ActionResult Index()
{
    var externalLinks = _configuration.GetSection("ExternalLinks");

    string userAgent = Request.Headers["User-Agent"];

    if (userAgent.Contains("Android"))
    {
        return Redirect(externalLinks["PlayStore"]);
    }
    else if (userAgent.Contains("iPhone"))
    {
        return Redirect(externalLinks["AppStore"]);
    }
    else
    {
        return Redirect(externalLinks["OtherDeviceUrl"]);
        //return Redirect("https://cors-anywhere.herokuapp.com/https://google.com");
    }
}

With Program.cs including:

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAllOrigins", builder =>
    {
        builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
    });
});

var app = builder.Build();

app.UseRouting();

app.UseCors("AllowAllOrigins");

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{ endpoints.MapControllers(); });

app.UseHttpsRedirection();

app.MapControllers();

app.Run();

I keep encountering this error:

Error with normal link

Also as you can see I've tried to use

return Redirect("https://cors-anywhere.herokuapp.com/https://google.com");

And I get an http 403 forbidden error instead.

0

There are 0 best solutions below