如何使我的ASP.NET应用程序仅通过HTTPS提供页面?

时间:2010-06-04 17:56:33

标签: asp.net https web-config

我希望我的应用程序通过SSL提供所有网页,所以我添加了行......

<secureWebPages enabled="true">
<directory path="." />
</secureWebPages>

...到我的Web.config,结果编译错误是:

  

Build(web):无法识别的配置部分secureWebPages。

我正在运行Visual Studio 2008

2 个答案:

答案 0 :(得分:5)

听起来您可能需要添加相应的configSections ...

<configuration>
  <configSections>
    <!-- modify as you need. -->
    <section 
        name="secureWebPages" 
        type="Hyper.Web.Security.SecureWebPageSectionHandler, 
        WebPageSecurity" 
        allowLocation="false" />
  </configSections>


   <secureWebPages mode="On" > 
    <directories>
        <add path="/" recurse="True" />
    </directories>
</secureWebPages>

答案 1 :(得分:3)

如果您想要一个简单,快速的解决方案适用于整个Web应用程序,可以将其添加到Application_BeginRequest文件中的Global.asax方法。

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
...
    If Request.IsSecureConnection = False Then
        Dim ub As New UriBuilder(Request.Url)
        ub.Scheme = Uri.UriSchemeHttps
        ub.Port = 443
        Response.Redirect(ub.Uri.ToString, True)
    End If
...
End Sub