-
Notifications
You must be signed in to change notification settings - Fork 25.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforcing SSL #135
Comments
This is how I'm currently implementing an enforcement of SSL, registering it as a piece of middleware. Granted I could probably improve things by stripping out the searching for localhost and instead use the new environment variables, but sometimes in production I will need to check on the local server and this prevents the SSL cert warnings, minor point so might be safer to move to environment variables. public async Task Invoke(HttpContext context)
{
var req = context.Request;
var portDelim = req.Host.ToUriComponent().IndexOf(":", StringComparison.OrdinalIgnoreCase);
var host = portDelim != -1 ? req.Host.ToUriComponent().Substring(0, portDelim) : req.Host.ToUriComponent();
if (req.IsHttps || host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
await _next.Invoke(context);
else
{
//If it's good enough for MS :)
//https://github.com/aspnet/Mvc/blob/046cb976b3e899052a95387b72ea4bee6987bff0/src/Microsoft.AspNet.Mvc.Core/RequireHttpsAttribute.cs
var newUrl = string.Concat(
"https://",
req.Host.ToUriComponent(),
req.PathBase.ToUriComponent(),
req.Path.ToUriComponent(),
req.QueryString.ToUriComponent());
context.Response.Redirect(newUrl, true);
}
} |
@Jak893 if you just want to enforce SSL for your MVC app, you can do that in Startup.ConfigureServices
|
@Rick-Anderson That solution works great. For users of 1.0.0 onwards it is now...
|
that won't force https for static files though |
@danroth27 any suggestions for forcing SSL on static files? |
Actually I guess redirection at the mvc level might be sufficient since static files should get requested using the same base url as the page itself... |
localhost/IIS Express SSL will use 443xx and that all works fine with the code above. I've been using this approach for years. |
We think we will handle this as part of #977 |
There is a very easy solution to set up Visual Studio 2017 RC2 to run on HTTPS.
|
We have content on this already here: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm#require-ssl |
Actually, it would be better to pull this content out as another article and just link to it. |
ASP.NET MVC -> Security -> Enforcing SSL
The text was updated successfully, but these errors were encountered: