Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[ASP.NET Core] Enable HTTPS rewrite
...
using Microsoft.AspNetCore.Mvc
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Primitives;
namespace BrunoMartinsPro.Startup
{
public class Startup
{
public IConfigurationRoot Configuration { get; }
private const string XForwardedPathBase = "X-Forwarded-PathBase";
private const string XForwardedProto = "X-Forwarded-Proto";
public Startup(IHostingEnvironment env)
{
...
}
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All
});
////Use only when having a reverse proxy configured
//app.Use((context, next) =>
//{
// if (context.Request.Headers.TryGetValue(XForwardedPathBase, out StringValues pathBase))
// {
// context.Request.PathBase = new PathString(pathBase);
// }
// if (context.Request.Headers.TryGetValue(XForwardedProto, out StringValues proto))
// {
// context.Request.Protocol = proto;
// ////Possible different configuration
// //context.Request.Scheme = proto;
// }
// return next();
//});
app.UseRewriter(new RewriteOptions().AddRedirectToHttps());
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment