【问题标题】:HTTP response is 404 rather than 401 with authorization.json Am I missing something?HTTP 响应是 404 而不是 401 with authorization.json 我错过了什么吗?
【发布时间】:2019-04-19 20:02:05
【问题描述】:

在 Azure 应用服务论坛中发布此内容并没有收到任何回复,所以...

框架是 4.7.1

应用程序是带有两个 Web API 控制器的 ASP.Net Web 表单。
网站和 HealthCheck 控制器应该允许匿名请求。 另一个控制器应使用证书作为凭据通过 Azure Active Directory 进行身份验证。

应用服务中的身份验证设置为

  • 允许匿名请求(无操作)
  • 身份验证提供程序设置为 Azure Active Directory
  • 管理模式设置为 Express
  • 证书已添加到管理应用程序中

唯一的问题是,当匿名请求到达安全控制器时,会返回 HTTP 404 而不是 401。

如果发出经过身份验证的请求,则返回 HTTP 200。 我是否缺少一些配置设置?

这是 Application_Start 中 Global.asax.cs 中的路线图

RouteTable.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = System.Web.Http.RouteParameter.Optional }
);

这里是authorization.json文件

{
  "routes": [{
      "path_prefix": "/",
      "policies": { "unauthenticated_action": "AllowAnonymous" }
    },
    {
      "path_prefix": "/api",
      "policies": { "unauthenticated_action": "RejectWith401" }
    },
    {
      "http_methods": [ "GET"],
      "path_prefix": "/api/HealthCheck",
      "policies": { "unauthenticated_action": "AllowAnonymous" }
    }]
}           

【问题讨论】:

    标签: azure authentication webforms asp.net-web-api2 azure-web-app-service


    【解决方案1】:

    您的文件 authorization.json 一定有问题。除了这个 /api 之外还有其他子目录吗?尝试添加它们,在您的path_prefix 中添加/api/(*whatever path*)

    【讨论】:

    • 这是 ASP.Net Web 表单应用程序中的 Web.API。没有 API 文件夹,只有处理路由的控制器。我已经尝试修改 authorization.json 以指示每个控制器上的响应应该是什么,结果是相同的。
    【解决方案2】:

    问题在于表单身份验证重定向检测到 401 并尝试重定向到表单元素的 loginUrl 属性中未正确指定的登录页面。 global.aspx.cs 文件的 Application_BeginRequest 中的这段代码修复了我的问题

                var context = new HttpContextWrapper(Context);
            // set flag only if forms auth enabled and request comes from ajax
            if (FormsAuthentication.IsEnabled && context.Request.Url.AbsolutePath.StartsWith("/api",StringComparison.CurrentCultureIgnoreCase))
            {
                context.Response.SuppressFormsAuthenticationRedirect = true;
            }
    

    在 Azure 中打开详细日志记录(由 Microsoft 建议)表明身份验证返回 401。那时我知道正在发生重定向,并且可能是表单身份验证。这个链接也帮助我找出了修复方法

    https://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx/

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 2017-05-16
      • 2016-07-20
      • 2016-04-27
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多