简体   繁体   English

IIS7使用外部IP阻止对asp.net应用程序的请求

[英]IIS7 Block request to asp.net app using external IP

I have a asp.net app (IIS7), and I want block access to it using external server IP. 我有一个asp.net应用程序(IIS7),我想使用外部服务器IP阻止对其进行访问。 I only want to allow access using my domain. 我只想允许使用我的域进行访问。

For example, my domain is domain.com and IP 161.0.0.1 and I want to block the access to http://161.0.0.1/webapp/ 例如,我的域是domain.com和IP 161.0.0.1,并且我想阻止对http://161.0.0.1/webapp/的访问

I prefer do it using web.config 我更喜欢使用web.config

Thx in advance, 提前谢谢

In IIS you configure exactly what IP / DNS name combination you want the site to respond to. 在IIS中,可以准确配置要站点响应的IP / DNS名称组合。 You can easily force it to only respond on a particular IP. 您可以轻松地强制它仅对特定IP做出响应。

For IIS 7: 对于IIS 7:

  1. Open the Internet Information Services (IIS) Manager 打开Internet信息服务(IIS)管理器
  2. Expand Sites and right click on your website. 展开站点,然后右键单击您的网站。
  3. Click on Edit Bindings. 单击编辑绑定。
  4. Edit the existing entry and set the IP address to 161.0.0.1. 编辑现有条目,并将IP地址设置为161.0.0.1。 Also set the domain name to domain.com. 还将域名设置为domain.com。
  5. Click OK, the Click Close. 单击确定,单击关闭。

Now your site wil only respond to that particular domain name and won't respond via IP address only. 现在,您的站点将仅响应该特定域名,而不会仅通过IP地址响应。

If your site uses an SSL certificate then see the following question which talks about how to configure IIS to force the hostname to be used: 如果您的站点使用SSL证书,请参阅以下问题,该问题讨论如何配置IIS以强制使用主机名:

https://serverfault.com/questions/96810/iis7-cant-set-host-name-on-site-with-ssl-cert-and-port-443 https://serverfault.com/questions/96810/iis7-cant-set-host-name-on-site-with-ssl-cert-and-port-443
which links to: 链接到:
http://www.sslshopper.com/article-ssl-host-headers-in-iis-7.html http://www.sslshopper.com/article-ssl-host-headers-in-iis-7.html

This link is even better for doing it entirely through the UI: http://blog.armgasys.com/?p=80 此链接甚至更适合完全通过UI进行: http : //blog.armgasys.com/?p=80

OK so if you want the Site to be accessible via DNS name but not via IP, the only way to distinguish that is to examine the requested host name in the header. 好的,因此,如果希望通过DNS名称而不是通过IP访问该站点,则唯一的区分方法是检查标头中的请求主机名。 There are two ways to do that I know of: 我知道有两种方法可以做到:

1) Configure Bindings dialog in IIS Manager. 1)在IIS管理器中配置“绑定”对话框。 This is the easiest to set up but doesn't work for HTTPS. 这是最简单的设置,但不适用于HTTPS。 Just put www.domain.com into the hostname field and requests to the IP will be rejected. 只需将www.domain.com放入主机名字段,对IP的请求将被拒绝。 For HTTPS if your security certificate is for a specific hostname, the user will get a security warning if they try to connect via IP, but typically they can override the warning (depending on browser settings). 对于HTTPS,如果您的安全证书是针对特定主机名的,则用户尝试通过IP连接时会收到安全警告,但通常他们可以覆盖该警告(取决于浏览器设置)。

Edit: Chris Lively has linked to a way to make this method work for HTTPS bindings as well, see his answer for more information. 编辑:克里斯·利弗利(Chris Lively)链接到一种使该方法也可用于HTTPS绑定的方法,有关更多信息,请参见他的答案。

2) Alternately you can examine the header in code. 2)或者,您可以检查代码中的标头。 Here is an example of an IHttpModule which accomplishes what you want. 这是一个IHttpModule的示例,可以完成您想要的操作。 It is also a drop-in solution that is configured in web.config. 这也是在web.config中配置的嵌入式解决方案。

Code: 码:

Public Class HostNameCheck
    Implements IHttpModule

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub

    Public Sub Init(context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf context_BeginRequest
    End Sub

    Private Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim objApp As HttpApplication = DirectCast(sender, HttpApplication)

        If objApp.Request.Url.Host <> ConfigurationManager.AppSettings("AcceptedHostName") Then
            objApp.Response.Clear()
            objApp.Response.StatusCode = 403
            objApp.Response.SubStatusCode = 6
            objApp.Response.Flush()
        End If
    End Sub
End Class

Web.config: Web.config:

<configuration>
    <appSettings>
        <add key="AcceptedHostName" value="www.domain.com"/>
    </appSettings>
    <system.webServer>
        <modules>
            <add name="HostNameCheck" type="HostNameCheck"/>
        </modules>
    </system.webServer>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM