我正在开发一个与安全的 .net 服务器通信的 PhoneGap 应用程序。问题是,我似乎无法通过任何请求(W3C)传递任何 Cookie。
这就是我正在做的事情(假设“用户名”和“密码”有效)。
var token;    
$.ajax({
        url: "https://server.com/AuthService/api/account/login",
        crossDomain: true,
        type: 'post',
        async: false,
        data: {
            username: "username",
            password: "password"
        }
    }).done(function(response) {
        token = response.securityToken;
        success = true;
    });
此时,我有一个身份验证令牌,可用于验证所有后续请求。所以使用该令牌我向服务器发出另一个请求......
$.ajax({
    url: "https://server.com/Service/api/search?query=" + query,
    headers: { Cookie: 'auth=' + token },
    crossDomain: true,
    withCredentials: true,
    type: 'POST',
    async: false,
    data: ' ' //because we don't want our request to be 0 bytes (the server is picky)
}).done(function(data) {
    result = data;
});
Chrome 只是说:拒绝设置不安全的标头“Cookie”(符合 W3C 规范)。该应用程序未设置标头,因此请求 401s,因为未发送授权 cookie。
我的问题是:有什么方法可以颠覆这一点并覆盖 PhoneGap 上的 Cookie 标头(或完全解决它的另一种方法)?我知道使用 Authorization 标头也是一种选择,但我对服务器的访问权限有限(它不支持它)并且希望有一个更直接的解决方案。
额外的问题:对 AuthService 的调用还应该在设备上设置一个 httpOnly cookie,但没有(我推测这是因为它是跨域请求)......我在这个假设中是否正确,或者可能有什么问题服务器端?
谢谢!