Skip to content

Instantly share code, notes, and snippets.

@brunomartinspro
Last active July 9, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomartinspro/f0982a177da00aec0ab43e2bc9aec593 to your computer and use it in GitHub Desktop.
Save brunomartinspro/f0982a177da00aec0ab43e2bc9aec593 to your computer and use it in GitHub Desktop.
[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