如何在 NWebsec.Core.Exceptions.RedirectValidationException 之后重定向到错误控制器

How to Redirect to Error controller after NWebsec.Core.Exceptions.RedirectValidationException

我有 ASP.NET MVC 5 应用程序,但它是否是 MVC 3、4 并不重要。我安装了启用了重定向验证的 NWebSec 3.0 模块。我有 Application_Error 方法(即使我没有,问题也是一样的!):

    protected void Application_Error(object sender, EventArgs e)
    {
        HttpContext httpContext = ((MvcApplication)sender).Context;
        Exception exception = Server.GetLastError();
        if(exception is NWebsec.Core.Exceptions.RedirectValidationException)
        {
            Trace.WriteLine("How to redirect from here to ErrorController/Index?");
        }
        else
        {
            Trace.WriteLine("How to redirect from here to ErrorController/Index?");

这是 HomeControllers 中的两个测试方法:

    public ActionResult Redirect()
    {
        return new RedirectResult("http://www.deshow.net/d/file/travel/2010-04/bing-landscape-wallpaper-845-2.jpg");
    }

    public ActionResult ParseError()
    {
        int.Parse("test");
        return View();
    }

而且我有标准的共享错误视图和

 <customErrors mode="On" defaultRedirect="~/Error" />

启用。

我希望无论发生什么错误我都会被重定向到错误视图。但当我调用 Redirect 方法时,情况并非如此。不知何故,当抛出重定向错误时,标准 'custom errors' 机制被绕过并且不显示标准错误视图。

另一个细节是,在 ParseError() 被命中后,Application_Error 没有被命中,标准的 'custom errors' 视图被显示,但是当 Redirect() 被命中时,没有 'custom errors'查看但死机黄屏。这让我觉得我可能没有正确理解 http 模块的 'pipeline'。

NWebSec 与错误的重定向相交并抛出 NWebsec.Core.Exceptions.RedirectValidationException。我想是因为 NwebSec 被注册为一个模块

    <system.webServer>
    <modules>
      ...
    <add name="NWebsecHttpHeaderSecurityModule" type="NWebsec.Modules.HttpHeaderSecurityModule, NWebsec, Version=3.2.0.0, Culture=neutral, PublicKeyToken=3613da5f958908a1" />
    ...
    </modules>
    </system.webServer>

我已经阅读了 the-e-e-e-e-s 个答案,但我无法理解。您能否指出此类问题的答案或提供示例实施?

我运行进入同样的问题。 documentation 解释说 3.x 版本在重定向上略有投机取巧。因此,您必须将批准的 url 重定向列入白名单(也允许自定义端口)。

在您的配置中,更新 allowSameHostRedirectsToHttps 节点。

<redirectValidation enabled="true">
  <allowSameHostRedirectsToHttps enabled="true" />
  <!--<allowSameHostRedirectsToHttps enabled="true" httpsPorts="4567, 7654"/>-->
  <add allowedDestination="http://www.nwebsec.com/"/>
  <add allowedDestination="https://www.google.com/accounts/"/>
</redirectValidation>

错误可能是由于在 headers 发送之前抛出的错误而抛出的。文档说,如果您想解决自己的自定义错误,可以调用 Response.Flush()。

假设您在 ErrorsController 上有一个 RedirectValidation 操作,这样的事情应该有效:

protected void Application_Error(object sender, EventArgs e)
{
    var routeData = new RouteData();
    routeData.Values["controller"] = "error";

    var exception = Server.GetLastError();

    if(exception is NWebsec.Core.Exceptions.RedirectValidationException)
    {
        routeData.Values["action"] = "redirectvalidation";
    }
    else
    {
        // handle everything else
    }

    Response.Clear();
    Server.ClearError();

    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;

    IController errorsController = new Controllers.ErrorController();
    HttpContextBase wrapper = new HttpContextWrapper(Context);

    var rc = new RequestContext(wrapper, routeData);
    errorsController.Execute(rc);
}

这基本上与您的一个链接答案所做的相同,只是您处理的是非常具体的异常类型。对于超出我们的上传大小限制导致的异常,我做了类似的事情。