使用web.config禁止用户代理

时间:2009-07-23 16:27:01

标签: asp.net iis

是否可以直接从web.config禁止某些用户代理?某些机器人似乎不遵循robots.txt,并且为了避免无意义的服务器负载(以及日志文件垃圾邮件),我想阻止某些类别的请求(特别是基于用户代理或非常可能是IP地址)继续进行

如果您知道是否可以防止此类请求完全记录到IIS的日志文件中,则可以获得奖励积分。 (即if-request-match,转发到/ dev / null,如果你理解我的意思)。

win2003的解决方案更可取,但这是一个反复出现的问题 - 如果有一个干净的IIS7解决方案而不是IIS6,我很乐意知道它。

编辑:很抱歉'早些时候我提到了一个不完整的问题,我偶然输入了标签+。

3 个答案:

答案 0 :(得分:12)

使用IIS7中的URLRewrite模块可以很容易地完成此操作。但我真的不知道这是否会阻止这些请求被记录。

 <rewrite> 
  <rules> 
    <rule name="Ban user-agent RogueBot" stopProcessing="true"> 
      <match url=".*" /> 
      <conditions> 
        <add input="{HTTP_USER_AGENT}" pattern="RogueBotName" /> 
        <add input="{MyPrivatePages:{REQUEST_URI}}" pattern="(.+)" /> 
      </conditions> 
      <action type="AbortRequest" /> 
    </rule> 
  </rules> 
  <rewriteMaps> 
    <rewriteMap name="MyPrivatePages"> 
      <add key="/PrivatePage1.aspx" value="block" /> 
      <add key="/PrivatePage2.aspx" value="block" />
      <add key="/PrivatePage3.aspx" value="block" /> 
    </rewriteMap> 
  </rewriteMaps> 
</rewrite>

答案 1 :(得分:3)

你可以编写一个自定义ASP.Net HttpModule,就像我为我的网站所做的那样禁止一些流氓机器人。这是代码:

public class UserAgentBasedRedirecter : IHttpModule
{
    private static readonly Regex _bannedUserAgentsRegex = null;
    private static readonly string _bannedAgentsRedirectUrl = null;

    static UserAgentBasedRedirecter()
    {
            _bannedAgentsRedirectUrl = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.RedirectUrl"];
            if (String.IsNullOrEmpty(_bannedAgentsRedirectUrl))
                _bannedAgentsRedirectUrl = "~/Does/Not/Exist.html";

            string regex = ConfigurationManager.AppSettings["UserAgentBasedRedirecter.UserAgentsRegex"];
            if (!String.IsNullOrEmpty(regex))
                _bannedUserAgentsRegex = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
    }

    #region Implementation of IHttpModule

    public void Init(HttpApplication context)
    {
            context.PreRequestHandlerExecute += RedirectMatchedUserAgents;
    }

    private static void RedirectMatchedUserAgents(object sender, System.EventArgs e)
    {
            HttpApplication app = sender as HttpApplication;

            if (_bannedUserAgentsRegex != null &&
                app != null && app.Request != null && !String.IsNullOrEmpty(app.Request.UserAgent))
            {
                if (_bannedUserAgentsRegex.Match(app.Request.UserAgent).Success)
                {
                    app.Response.Redirect(_bannedAgentsRedirectUrl);
                }
            }
    }

    public void Dispose()
    { }

    #endregion
}

您需要在web.config中注册它并指定用于匹配用户代理字符串的正则表达式。这是我用来禁止msnbot / 1.1流量的那个:

<configuration> 
    <appSettings>
        <add key="UserAgentBasedRedirecter.UserAgentsRegex" value="^msnbot/1.1" />
    </appSettings>
...
    <system.web>
        <httpModules>
            <add name="UserAgentBasedRedirecter" type="Andies.Web.Traffic.UserAgentBasedRedirecter, Andies.Web" />
        </httpModules>
    </system.web>
</configuration>

答案 2 :(得分:-1)

不要认为你可以从web.config执行此操作(web.config中的授权适用于用户,而不是机器人)。您最好的选择是IIS本身的某种自定义ISAPI过滤器。有一个blog about this here。祝你好运!