【问题标题】:IISNode turning off custom errors with httpErrors="Passthrough" not workingIISNode 使用 httpErrors="Passthrough" 关闭自定义错误不起作用
【发布时间】:2020-10-14 06:36:48
【问题描述】:

我正在使用中间件模块来验证 JWT 令牌。当我尝试将状态代码设置为 401 或 403 时,我要么得到内置的 IIS 错误 HTML,要么将 httpErrors 设置为“Passthrough”。我假设如果我使用 404 或 500 也会遇到同样的问题。当我在 ASP.NET MVC 中使用不同的 API 时尝试设置状态代码并返回 JSON 时,我遇到了类似的问题。我用这篇文章来帮助我做对了:https://weblog.west-wind.com/posts/2017/jun/01/bypassing-iis-error-messages-in-aspnet。问题是我在运行 IIS 的本地机器上运行相同的代码时没有问题。这与我要部署到的服务器或 IIS 的配置方式有关。我还需要在 IIS 中做些什么才能使其正常工作吗?我在 IIS 中将其作为应用程序/虚拟目录运行是否重要?

这是 JS 代码:

const aad = require('azure-ad-jwt');

module.exports = (req, res, next) => {
    console.log('authorizing');
    if(!req.headers.authorization){
        res.status(403).json({
            message: "Auth failed"
        });
        return;
    }

    const jwtToken = req.headers.authorization.replace('Bearer ', '');

    aad.verify(jwtToken, null, function (err, result) {
        if (result) {
            const scopes = result.scp.split(',');
            if (!scopes.includes("Prep.API")) {
                res.status(401).json({
                    message: "Auth failed"
                });
            } else {
                next();
            }
        } else {
            res.status(401).json({
                message: "Auth failed"
            });
        }
    });
};

当我有这样的 web.config 时:

<configuration>
    <appSettings>
        <add key="virtualDirPath" value="/myapp" />
    </appSettings>
    <system.web>
        <customErrors mode="Off" />
    </system.web>
    <system.webServer>
        <!--<httpErrors existingResponse="Passthrough" />-->
        <iisnode
                    watchedFiles="web.config;*.js;api///*.js" />
        <handlers>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="api">
                    <match url="/*" />
                    <action type="Rewrite" url="server.js" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

回复是:

当我取消注释 httpErrors 行时,web.config 如下所示:

<configuration>
    <appSettings>
        <add key="virtualDirPath" value="/myapp" />
    </appSettings>
    <system.web>
        <customErrors mode="Off" />
    </system.web>
    <system.webServer>
        <httpErrors existingResponse="Passthrough" />
        <iisnode
                    watchedFiles="web.config;*.js;api///*.js" />
        <handlers>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="api">
                    <match url="/*" />
                    <action type="Rewrite" url="server.js" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

回复是:

那么我做错了什么或者我还能做些什么来获得正确的状态代码以及 JSON 响应?如果不出意外,如何让 IIS 报告错误,以便在事件查看器中看到异常或其他内容?

【问题讨论】:

  • 你可能会注意到 iisnode 已经死了好几年了。你花在它上面的任何时间都可能被浪费掉。
  • 在 IIS 上运行节点应用程序有更好的选择吗?
  • 谢谢!我看过类似这样的文章:medium.com/@harshamw/…。不知道该怎么做。我现在就试试。

标签: node.js iis iis-7.5 iis-6 iisnode


【解决方案1】:

PassThrough 一定要Pascal 大小写,T 字母要大写。

以下行对我来说很好。

<httpErrors existingResponse="PassThrough" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多