Web API OWIN从$ .AJAX POST withCredentials接收空数据:true

问题描述:

我正在通过OWIN调用Windows服务上托管的Web API,以及来自ASP.NET MVC应用程序的jquery ajax post(通过'data ' 选项)。它一直在工作,直到我决定添加集成Windows身份验证。我在$ .ajax调用中添加了xhrFields:{withCredentials:true}来完成客户端身份验证。现在返回的数据为空。

这里是我的服务器端启动:

public class Startup 
{ 
    public void Configuration(IAppBuilder appBuilder) 
    { 
     var config = new HttpConfiguration(); 

     var listener = 
      (HttpListener)appBuilder.Properties["System.Net.HttpListener"]; 
     listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication; 

     //Maps Http routes based on attributes 
     config.MapHttpAttributeRoutes(); 
     config.Filters.Add(new AuthorizeAttribute()); 

     //Enable WebApi 
     var cors = new EnableCorsAttribute("*", "*", "*"); 
     cors.SupportsCredentials = true; 
     config.EnableCors(cors); 

     appBuilder.UseWebApi(config); 
    } 
} 

这里是Web API方法:

公共HttpResponseMessage PostSomething([FromBody]字符串数据输入)

仅供参考,串可太大的URI被传递。

这里是$就电话:

function TestAjax() { 
     $.ajax({ 
      url: 'http://localhost:8080/api/Test/PostSomething', 
      xhrFields: { withCredentials: true }, 
      type: 'post', 
      data: 'test' 
     }).done(function (response) { 
      alert(response); 
     }); 
    } 

数据输入总是空。

嗯,我找到了答案,这很简单,但我仍然不知道幕后的细节为什么这会起作用。简单地修改“data:'test'”为“data:{'':'test'}”。

function TestAjax() { 
    $.ajax({ 
     url: 'http://localhost:8080/api/Test/PostSomething', 
     xhrFields: { withCredentials: true }, 
     type: 'post', 
     data: { '': 'test' } 
    }).done(function (response) { 
     alert(response); 
    }); 
} 

如果有人知道为什么,请发表回复。谢谢!