【问题标题】:How do I make my ASP.NET application serve pages only over HTTPS?如何使我的 ASP.NET 应用程序仅通过 HTTPS 提供页面?
【发布时间】:2011-02-27 22:13:33
【问题描述】:
我希望我的应用程序通过 SSL 为其所有网页提供服务,所以我添加了这些行...
<secureWebPages enabled="true">
<directory path="." />
</secureWebPages>
... 到我的 Web.config 并且生成的编译器错误是:
构建(网络):无法识别的配置部分secureWebPages。
我正在运行 Visual Studio 2008
【问题讨论】:
标签:
asp.net
https
web-config
【解决方案1】:
听起来您可能需要添加适当的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>
【解决方案2】:
如果您想要一个简单、快速的解决方案适用于您的整个 Web 应用程序,您可以将其添加到您的 Global.asax 文件中的 Application_BeginRequest 方法中。
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