Asp.net mvc 如何在web.config中阻止来自特定域的请求?

Asp.net mvc 如何在web.config中阻止来自特定域的请求?,asp.net-mvc,web-config,Asp.net Mvc,Web Config,我想在web.config中为我的应用程序限制一个域。我怎么做?我刚刚找到了IIS设置的解决方案,比如 <add ipAddress="some_ip_address" /> 但我想在IIS管理器中限制一个域,而不是IP地址: 选择所需的站点 在主窗格中,打开IP地址和域限制 在“操作”窗格中,启动“编辑功能设置…” 选中此框以启用域名限制 单击[确定]退出 现在,您可以像添加IP地址一样添加域名 <security> <i

我想在web.config中为我的应用程序限制一个域。我怎么做?我刚刚找到了IIS设置的解决方案,比如

<add ipAddress="some_ip_address" />

但我想在IIS管理器中限制一个域,而不是IP地址:

  • 选择所需的站点
  • 在主窗格中,打开IP地址和域限制
  • 在“操作”窗格中,启动“编辑功能设置…”
  • 选中此框以启用域名限制
  • 单击[确定]退出
  • 现在,您可以像添加IP地址一样添加域名

           <security>
               <ipSecurity allowUnlisted="true">    
               <clear/>     <!-- removes all upstream restrictions -->                
               <add ipAddress="83.116.19.53"/>                
               </ipSecurity>
          </security>
    
    注意:这不能通过web.config完成。这些设置存储在ApplicationHost.config中。您必须访问IIS管理器才能启用IP和域安全。这是没有办法的

    <ipSecurity enableReverseDns="true">
        <add ipAddress="192.168.0.1" allowed="false" />
        <add domainName="google.com" allowed="false" />
    </ipSecurity>
    

    web.config中没有直接设置来执行此操作。但是,您可以在web.config中创建自定义集合以列出域,并编写自定义requesthandler或自定义actionfilter来阻止特定域。然而,这很容易被欺骗或绕过。以下是一个例子:

    您的configsection类:

        public class DisallowedDomainsSection : ConfigurationSection
        {
            // Create a "remoteOnly" attribute.
            [ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
            public Boolean RemoteOnly
            {
                get
                { 
                    return (Boolean)this["remoteOnly"]; 
                }
                set
                { 
                    this["remoteOnly"] = value; 
                }
            }
    
            // Create a "domain" element.
            [ConfigurationProperty("domainName")]
            public DomainElement Domain
            {
                get
                { 
                    return (DomainElement)this["domainName"]; }
                set
                { this["domainName"] = value; }
            }
        }
    
    然后在configSections节点中声明配置节:

    <configSections>
        <sectionGroup name="disAllowDomainsGroup">
          <section 
            name="disallowDomains" 
            type="your.class.definition" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
      </configSections>
    
    然后在不允许的域中迭代并使用

    HttpContext.Current.Request.Url.Host
    

    将不允许的域与请求域匹配。但如前所述,这一点很容易被知道怎么做的人绕过

    没有直接方法不允许特定域。您只能限制某些IP。 您可以始终使用nsloopkup将域映射到其IP地址

           <security>
               <ipSecurity allowUnlisted="true">    
               <clear/>     <!-- removes all upstream restrictions -->                
               <add ipAddress="83.116.19.53"/>                
               </ipSecurity>
          </security>
    
    
    
    您是否知道按域名进行限制需要对每个请求进行rDNS查找?我没有访问IIS管理器的权限。客户要求-通过web.config执行这些设置不存储在web.config中。它们存储在(默认)c:\windows\system32\inetsrv\config中的applicationhost.config中。我将在上面的回答中添加相关行。
           <security>
               <ipSecurity allowUnlisted="true">    
               <clear/>     <!-- removes all upstream restrictions -->                
               <add ipAddress="83.116.19.53"/>                
               </ipSecurity>
          </security>