You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core AllowedHosts功能失效及IIS URL重写500错误求助

问题

我基于.NET Core 3构建了一个微服务,部署在Azure虚拟机的ATIKA域名下。我希望仅允许IVKLA域名访问该API,其他域名的请求返回400状态码。

我已在appsettings文件中配置:

"AllowedHosts": "IVKLA.com"

但来自IVKLA域计算机的Postman请求仍返回400状态码,尝试论坛相关方案后问题仍未解决。

以下信息最后更新于2022年10月21日

按照建议,我尝试在IIS服务器中使用URL Rewrite模块:

  • 能够成功拦截IP并返回403禁止状态;
  • 配置IP白名单时出现“500 URL Rewrite Module Error”错误。

以下是web.config中的URL重写规则:

<rewrite>
        <rewriteMaps>
            <rewriteMap name="Authorized Admin IPs">
                <add key="1.2.3.4" value="1" />
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="Authorized admin" patternSyntax="Wildcard">
                <match url="*" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{Authorized Admin IPs:{REMOTE_ADDR}}" pattern="1" />
                    <add input="{REMOTE_ADDR}" pattern="*.*.*.*" />
                </conditions>
                <serverVariables>
                    <set name="HTTP_X_AUTHORIZED_ADMIN" value="yes" />
                </serverVariables>
                <action type="None" />
            </rule>
            <rule name="un Authorized users" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern=".*" />
                    <add input="{HTTP_X_AUTHORIZED_ADMIN}" pattern="yes" />
                </conditions>
                <action type="CustomResponse" statusCode="403" statusReason="forbidden" statusDescription="forbidden" />
            </rule>
        </rules>
    </rewrite>

内容的提问来源于stack exchange,提问作者Sid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:45:33