我能否从网站代码中检测 ASP.NET 网站是否启用了 SSL/https?

发布于 2024-10-02 17:32:41 字数 579 浏览 2 评论 0原文

我正在开发一个 CMS,它可以在网络服务器上启用或不启用 https 的情况下运行。我希望能够检测是否启用了 https,以便我可以采取相应的行动(例如,向管理员显示一些与 https 相关的选项,并重定向到 https 以供管理员登录)。

我不是在寻找 Request.IsSecureConnection 因为它只告诉我当前请求是否是通过 https 进行的。我想要一些东西来告诉我 IIS 中站点的当前绑定是否包含与当前请求处于同一域的 https 绑定。因此,例如,即使当前请求是针对 http://example.com/ 的,因此不安全,我想知道 https://example.com/ 是否code> 可以工作,因此如果用户以管理员身份登录,我可以(例如)将用户重定向到它。

不过,我没有在 System.Web.Configuration 中找到任何可以告诉我当前站点绑定的信息。

我当前的解决方法只是要求管理员在启用 https 的情况下在 web.config 中设置 appSetting,但我更希望能够使其自动执行。必须两次设置相同的内容 - 一次在 IIS 中,一次在 web.config 中 - 令人困惑。

I'm working on a CMS that can run either with or without https enabled on the webserver. I'd like to be able to detect whether https is enabled or not, so that I can act accordingly (for example, display some https-related options to the administrator, and redirect to https for administrator logins).

I'm not looking for Request.IsSecureConnection because that only tells me if the current request is via https. I want something that will tell me whether the current bindings for the site in IIS include a binding for https at the same domain as the current request is on. So, for example, even if the current request is for http://example.com/ and thus not secure, I want to know whether https://example.com/ would work so I can (for example) redirect the user to it if they log in as administrator.

I've had no luck looking for anything in System.Web.Configuration that will tell me about the bindings of the current site, though.

My current workaround is just to require the administrator to set an appSetting in web.config if https is enabled, but I'd prefer if I could make it automatic. Having to set the same thing twice - once in IIS and once in web.config - is confusing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-10-09 17:32:41

最简单的方法是从站点向该站点发出 https 请求,如果成功,那么您就知道 https 受到支持。将其缓存在静态变量中,以便每次应用程序调用仅调用一次。

The simplest way is to make an https request to the site from the site and if it succeeds then you know https is supported. Cache this in a static variable so it's only called once per app invocation.

始终不够爱げ你 2024-10-09 17:32:41

根据您的 IIS 版本,您可以使用托管 .net 代码在 IIS7 中执行管理任务。这是查询站点的绑定以查看是否启用 https 的示例

http://msdn.microsoft.com/en-us/library/microsoft.web.administration.bindingcollection(v=VS.90).aspx

Depending on your IIS version you can use managemed .net code to do administrative tasks in IIS7. This is an example of querying a site for its bindings to see if https is enabled

http://msdn.microsoft.com/en-us/library/microsoft.web.administration.bindingcollection(v=VS.90).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文