在运行时获取 IIS 绑定
我想知道如何使用 ASP.NET 在运行时获取当前站点的 IIS 绑定设置(主机名、端口、IP 地址)。 .NET 是否提供任何方式来获取这些信息?
编辑:我需要一种方法来配置 http 和 https 端口,以便在从 http 切换到 https 时重定向到正确的端口,如果使用其他端口,则从 https 切换回 http,然后使用 80/443。有没有办法在没有扩展权限的情况下做到这一点?
问候
I wonder how to get the IIS binding settings of the current site (host name, port, IP address) at runtime using ASP.NET.
Does .NET provide any way to get these information?
Edit: I need a way to get the http and https ports configured to redirect to the right port when switching from http to https, and back from https to http if other ports then 80/443 are used. Is there a way to do this without extended privileges?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现这一目标的唯一方法(无需成为管理员)是使用 Microsoft.Web.Administration。我刚刚写了一篇关于如何做到这一点的快速博客,请参阅:
https://web.archive.org/web/20150722034236/http://blogs.msdn.com/b/carlosag/archive/2011/ 01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx
基本上,由于 IIS 有一个我们称为工作进程隔离的功能,因此可以从应用程序本身读取配置,而无需成为管理员的需要。如果您使用 ADSI、Metabase 或任何其他方式,您将需要成为管理员。
The only way to achieve that (without being an administrator) is using Microsoft.Web.Administration. I just wrote a quick blog on how to do that, see:
https://web.archive.org/web/20150722034236/http://blogs.msdn.com/b/carlosag/archive/2011/01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx
Basically since IIS has a feature we call Worker Process isolation it is possible to read the configuration from an Application itself without the need of being administrator. If you use ADSI, Metabase, or any other way, you will require being an administrator.
您应该能够通过使用 System.DirectoryServices 程序集访问 IIS 元数据库来完成此操作。
例如,您可以在此处枚举所有站点以及这些站点中包含的属性配置。
将此引用添加到您的项目中:
using System.DirectoryServices
You should be able to accomplish this by accessing the IIS metabase, using the System.DirectoryServices assembly.
For example, here you can enumerate through all of your sites and property configurations contained within those sites.
Add this reference to your project:
using System.DirectoryServices
您可以使用以下代码来获取绑定:
以下代码可用于测试上述方法:
使用的命名空间:
引用的程序集:Microsoft.Web.Administration
将上述代码放入 Sample.aspx 中测试一下:
会给你这样的输出:
You can use the following code to get the bindings:
Following code can be used to test the above method:
Namespaces used:
Assembly referred: Microsoft.Web.Administration
Putting the above code in Sample.aspx to test it:
Will give you output like this:
我认为您正在寻找元数据库访问:
I think you're looking for metabase access:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/43a51d34-7c81-413b-9727-ec9a19d0b428.mspx?mfr=true