我希望我的用户仅通过 SSL 访问特定的 asp.net 虚拟目录。 web.config 中是否有允许我指定的选项?
最佳答案
您可以在 IIS7 中执行此操作。转到您的应用程序的 SSL 设置,您将看到带有“需要 SSL”选项的复选框,选中该复选框,您的工作就完成了。现在您的网站只能从 https 访问,而不能从 http 访问。 请记住,您需要拥有 SSL 证书,否则浏览器会针对您的网站显示一些警告消息。 是的……我不认为您可以使用 web.config 实现这一目标。
编辑:示例代码
可以使用 Global.asax 文件进行完全自定义。您可以添加特定条件并应用 https 或 http。下面的示例代码显示,如果页面是登录/结帐并且连接不安全,则从 http 重定向到 https,而且我可能不需要 https 作为联系页面。
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if ((Request.Path.EndsWith("login.aspx") || Request.Path.EndsWith("checkout.aspx") ) && !Request.IsSecureConnection)
{
Response.Redirect(Request.Url.AbsoluteUri.Replace("http:", "https:"));
}
else if (Request.IsSecureConnection && !Request.Path.EndsWith("contact.aspx"))
{
Response.Redirect(Request.Url.AbsoluteUri.Replace("https:", "http:"));
}
}
关于asp.net - 通过 web.config 禁用 HTTP 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857774/