带有 Windows 身份验证的 WebAPI CORS - 允许匿名 OPTIONS 请求 [英] WebAPI CORS with Windows Authentication - allow Anonymous OPTIONS request

查看:29
本文介绍了带有 Windows 身份验证的 WebAPI CORS - 允许匿名 OPTIONS 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Windows 身份验证运行的 WebAPI 2 REST 服务.它与网站分开托管,因此我使用 ASP.NET CORS NuGet 包启用了 CORS.我的客户网站正在使用 AngularJS.

I have a WebAPI 2 REST service running with Windows Authentication. It is hosted separately from the website, so I've enabled CORS using the ASP.NET CORS NuGet package. My client site is using AngularJS.

到目前为止,这是我所经历的:

So far, here's what I've been through:

  1. 我没有设置 withCredentials,因此 CORS 请求返回 401.通过将 withCredentials 添加到我的 $httpProvider 配置中解决.
  2. 接下来,我将 EnableCorsAttribute 设置为通配符来源,这在使用凭据时是不允许的.通过设置明确的来源列表来解决.
  3. 这使我的 GET 请求成功,但我的 POST 发出了预检请求,并且我没有创建任何控制器操作来支持 OPTIONS 动词.为了解决这个问题,我将 MessageHandler 实现为全局 OPTIONS 处理程序.它只是为任何 OPTIONS 请求返回 200.我知道这并不完美,但目前在 Fiddler 中有效.

我被困在哪里 - 我的 Angular 预检调用不包括凭据.根据 this answer,这是设计使然,因为 OPTIONS 请求被设计为匿名的.但是,Windows 身份验证使用 401 停止请求.

Where I'm stuck - my Angular preflight calls aren't including the credentials. According to this answer, this is by design, as OPTIONS requests are designed to be anonymous. However, the Windows Authentication is stopping the request with a 401.

我尝试将 [AllowAnonymous] 属性放在我的 MessageHandler 上.在我的开发计算机上,它可以工作 - OPTIONS 动词不需要身份验证,但其他动词需要.但是,当我构建并部署到测试服务器时,我的 OPTIONS 请求继续收到 401.

I've tried putting the [AllowAnonymous] attribute on my MessageHandler. On my dev computer, it works - OPTIONS verbs do not require authentication, but other verbs do. When I build and deploy to the test server, though, I am continuing to get a 401 on my OPTIONS request.

在使用 Windows 身份验证时,是否可以在我的 MessageHandler 上应用 [AllowAnonymous]?如果是这样,有关如何执行此操作的任何指导?或者这是错误的兔子洞,我应该寻找不同的方法?

Is it possible to apply [AllowAnonymous] on my MessageHandler when using Windows Authentication? If so, any guidance on how to do so? Or is this the wrong rabbit hole, and I should be looking at a different approach?

更新:我能够通过在 IIS 中的站点上同时设置 Windows 身份验证和匿名身份验证来使其工作.这导致一切都允许匿名,所以我添加了一个全局的 Authorize 过滤器,同时在我的 MessageHandler 上保留 AllowAnonymous.

UPDATE: I was able to get it to work by setting both Windows Authentication and Anonymous Authentication on the site in IIS. This caused everything to allow anonymous, so I've added a global filter of Authorize, while retaining the AllowAnonymous on my MessageHandler.

但是,这感觉像是一种 hack……我一直都明白,应该只使用一种身份验证方法(不能混合使用).如果有人有更好的方法,我将不胜感激.

However, this feels like a hack...I've always understood that only one authentication method should be used (no mixed). If anyone has a better approach, I'd appreciate hearing about it.

推荐答案

我使用了 HttpListener 的自托管,以下解决方案对我有用:

I used self-hosting with HttpListener and following solution worked for me:

  1. 我允许匿名 OPTIONS 请求
  2. 在 SupportsCredentials 设置为 true 的情况下启用 CORS

var cors = new EnableCorsAttribute("*", "*", "*");
cors.SupportsCredentials = true;
config.EnableCors(cors);
var listener = appBuilder.Properties["System.Net.HttpListener"] as HttpListener;
if (listener != null)
{
    listener.AuthenticationSchemeSelectorDelegate = (request) => {
    if (String.Compare(request.HttpMethod, "OPTIONS", true) == 0)
    {
        return AuthenticationSchemes.Anonymous;
    }
    else
    {
        return AuthenticationSchemes.IntegratedWindowsAuthentication;
    }};
}

这篇关于带有 Windows 身份验证的 WebAPI CORS - 允许匿名 OPTIONS 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆