什么会导致处理程序被忽略?

What can cause a handler to be ignored?

我们搬到了新服务器,我的 Thinktecture IdentityModel 东西坏了。

这是一个超级简化的重现样本。这在 运行 本地从 Visual Studio 工作,但部署到服务器显然没有处理程序。

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Thinktecture.IdentityModel.Tokens.Http;
namespace WebApplication1
{
  public class WebApiApplication : System.Web.HttpApplication
  {
    protected void Application_Start()
    {
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configuration.MessageHandlers.Add(
        new AuthenticationHandler(CreateConfiguration()));
      GlobalConfiguration.Configure(WebApiConfig.Register);
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
      BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    private AuthenticationConfiguration CreateConfiguration()
    {
      var config = new AuthenticationConfiguration
      {
        EnableSessionToken = true,
        RequireSsl = false,
        SendWwwAuthenticateResponseHeaders = false
      };    
      config.AddBasicAuthentication(
        (username, password) => { return username == password; });    
      return config;
    }
  }
}

处理程序没有执行。我已经设置了远程调试,这表明

此脚本为测试客户端

  <script>
    $(document).ready(function () {
      var u = "bilbo";
      var p = "bilbo";
      var btoken = btoa(u + ":" + p);
      $.ajax({
        url: "api/token",
        headers: { Authorization: "Basic " + btoken },
      }).then(function (result) {
        document.write("auth ok");
      }).fail(function (error) {
        document.write("auth fail");
      });
    });
  </script>

它生成一个 api/token 的请求,并带有基本身份验证 header,如图所示:

GET http://assa.com.au/api/token HTTP/1.1
Accept: */*
Authorization: Basic YmlsYm86YmlsYm8=
X-Requested-With: XMLHttpRequest
Referer: http://assa.com.au/sandpit
Accept-Language: en-AU,en-GB;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: assa.com.au
Connection: Keep-Alive

此服务器响应此 401

HTTP/1.1 401 Unauthorized
Content-Type: text/html
Server: Microsoft-IIS/8.5
WWW-Authenticate: Basic realm="assa.com.au"
X-Powered-By: ASP.NET
Date: Wed, 17 Feb 2016 01:36:27 GMT
Content-Length: 1293

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
  <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
 </fieldset></div>
</div>
</body>
</html>

Visual Studio 2013 在处理程序中显示有效断点,但未命中。这就是我认为没有调用处理程序的原因。

响应指定了一个领域,但修改处理程序注册以指定realm = "assa.com.au"不影响结果。

答案在于尾部斜杠缺失的处理。

测试页被请求为 assa.com.au/sandpit,它 return 正确 HTML。

仔细检查 401 响应发现请求是针对 api/token 的,这对于令牌分配器来说是不正确的 URL - 它应该是 sandpit/api/token

将测试页请求为 assa.com.au/sandpit/ 导致请求的 URL 变为 sandpit/api/token 并且一切都在清洗中。

但是为什么是401呢?不应该是404找不到吗?事实证明,网络服务器被配置为通过要求用户代理进行身份验证来响应未经授权的请求,表示为 401 身份验证需求。

不正确的 URL 使事物处于未授权状态,产生 401 身份验证需求。