Last active
July 9, 2018 22:11
-
-
Save brunomartinspro/f0982a177da00aec0ab43e2bc9aec593 to your computer and use it in GitHub Desktop.
[ASP.NET Core] Enable HTTPS rewrite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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