具有SupportsCredentials = true的WebAPI EnableCors无法正常工作

osc*_*asu 5 cors asp.net-web-api asp.net-web-api2

我已经部署了一个MVC站点,mysite.mydomain.co可以根据ADFS 进行身份验证并创建一个auth cookie:

public partial class Startup
{
    public void ConfigureUserAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
            });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                MetadataAddress = ConfigurationManager.AppSettings["adsfs.MetadataAddress"],
                Wtrealm = ConfigurationManager.AppSettings["adsfs.Wtrealm"]
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

在启用了CORS的“ myapi.mydomain.com”上还部署了一个WebAPI网站:

GlobalConfiguration.Configuration.EnableCors(new EnableCorsAttribute("https://mysite.mydomain.com", "*", "*") { SupportsCredentials = true });
Run Code Online (Sandbox Code Playgroud)

用户转到mysite.mydomain.com。MVC站点通过ADFS进行身份验证,我看到设置了身份验证cookie没问题。

我的应用程序主要是SPA,因此从javascript开始,有AJAX调用可以myapi.mydomain.com使用jQuery,将withCredentials选项设置为true

$.ajaxSetup({
    xhrFields: { withCredentials: true }
});
Run Code Online (Sandbox Code Playgroud)

该选项应该将安全凭证(cookie)发送到API。在运行时,我没有看到将cookie设置为API,并且按预期收到了401(未授权)错误。

如果我在localhost上运行相同的代码(当然,除了将源更改为localhost之外),我都会看到Cookie毫无问题地流入了API。我最好的猜测是它有效,因为它是相同的子域(localhost),而在我的服务器上是“ mysite”与“ myapi”。

有任何想法吗?