Ensure page is only accessed via SSL

2019-03-16 08:33发布

How do I ensure that my users can not physically type in http: to bypass my SSL and ensure that every page is https:?

Possibly a redirect on my master page?

标签: c# asp.net ssl
6条回答
叼着烟拽天下
2楼-- · 2019-03-16 08:34
if(!String.Equals(Request.Url.Scheme,
                  "https",
                  StringComparison.OrdinalIgnoreCase)) { }
查看更多
SAY GOODBYE
3楼-- · 2019-03-16 08:37

This would generally be handled via IIS configuration or with an ISAPI filter, but if you want to do it in the application code, you could put something like this in the Page_Init event of your master page...

If Not Request.IsSecure
    Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"))
End If
查看更多
老娘就宠你
4楼-- · 2019-03-16 08:45

I would just redirect all http urls to https with a separate page, or use the "require secure channel" option on your IIS configuration, which will display an error if someone tries to access a non-https page.

Here's a site with a guide to redirecting the error page to the https URL of your site.

查看更多
看我几分像从前
5楼-- · 2019-03-16 08:47

I've done this with an HTTPModule so that you don't have to worry about putting the code in every master page (if you have more than one). This version also turns off the redirect for localhost so you don't have to have SSL on your own machine. Basically you make a new HTTP module like this:

Public Class RedirectToHttpsModule
    Implements IHttpModule

    Public Sub Dispose() Implements IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf context_BeginRequest
    End Sub

    Private Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim application As HttpApplication = TryCast(sender, HttpApplication)
        If Not application.Request.IsSecureConnection And Not application.Request.IsLocal Then
             application.Response.Redirect(application.Request.Url.ToString().Replace(application.Request.Url.Scheme, "https"))
        End If
    End Sub

End Class

You also have to add the appropriate line in web.config for the HTTPModule:

<httpModules>
      <add type="RedirectToHttpsModule" name="RedirectToHttpsModule" />
</httpModules>
查看更多
走好不送
6楼-- · 2019-03-16 08:48

The following builds upon Josh Stodolas answer (IsSecureConnection) but uses the UriBuilder to change the scheme to https rather than a string replace. The benefit of this approach is that it won't change all the occurrences of "http" in the URL to "https".

if (!Request.IsSecureConnection)
{
    UriBuilder newUri = new UriBuilder(Request.Url);
    newUri.Scheme = Uri.UriSchemeHttps;
    Response.Redirect(newUri.Uri.AbsoluteUri);
}
查看更多
太酷不给撩
7楼-- · 2019-03-16 08:56

If you want to accept only secure connections, create a separate service for port 80 that only redirects to HTTPS. Ideally, you would preserve the requested path in the HTTP redirect.

If you simply want to encourage HTTPS connections for browsing (and don't care about robots, e.g.), add this to your pages:

<script type="text/javascript">
if(location.protocol=='http:')
  location=location.toString().replace(/^http:/,'https:');
</script>
查看更多
登录 后发表回答