Skip to content

Instantly share code, notes, and snippets.

@rmacfie
Last active May 14, 2016 11:22
Show Gist options
  • Save rmacfie/832038a8e2d5607da84b9534d9ab8493 to your computer and use it in GitHub Desktop.
Save rmacfie/832038a8e2d5607da84b9534d9ab8493 to your computer and use it in GitHub Desktop.
Enforce HTTPS in Asp.Net Core
using System;
namespace Microsoft.AspNet.Builder
{
public static class HttpsEnforcer
{
public static IApplicationBuilder UseHttpsEnforcer(this IApplicationBuilder app)
{
Func<RequestDelegate, RequestDelegate> middleware = next => async context =>
{
var req = context.Request;
var res = context.Response;
if (req.IsHttps)
await next(context);
else
res.Redirect($"https://{req.Host}{req.Path}{req.QueryString}");
};
return app.Use(middleware);
}
}
}
namespace MyProject
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseHttpsEnforcer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment