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:
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.