在PhoneGap/Cordova中处理cookie

Ber*_*rnd 84 cookies xmlhttprequest cordova

我正在开发一个使用服务器会话的PhoneGap应用程序.它需要cookie来处理会话.此外,还应处理来自负载均衡器的cookie.所以没有办法解决.你如何处理PhoneGap应用程序中的Cookie?

我已经完成了一些研究:

  • 有人说cookie处理可能取决于服务器没有为未知用户代理(IIS)设置cookie:iOS上的PhoneGap会话(cookie)
  • 在JavaScript中,可以使用document.cookie = ...设置cookie,但它们不会保存在PhoneGap中并丢失.在发射xhr请求之前它有效.
  • 使用xhr.getResponseHeader('Set-Cookie')xhr请求后可以检索Cookie.但只有当实际设置在服务器上时.不幸的是,jQuery剥离了"Cookie"标题.
  • 在(xhr)请求之后,未分配JavaScript document.cookie属性并且未更新.
  • 有人建议localStorage保存会话ID等.但是所有脚本都可以访问它,这可能是XSS安全问题.Cookie使用httponly标志解决此问题.
  • iOS:有一些修改会改变webView行为以支持cookie.但他们似乎无法使用iOS 6和PhoneGap 2.5:https://groups.google.com/forum/ fromgroups =#!topic / phonegap/ZJE1nxX63ow
  • 默认情况下,AppDelegate.m(v2.5)中似乎启用了Cookie.

Tia*_*vêa 67

朋友,我已经尝试过使用带有phonegap的cookie而没有成功.解决方案是使用localStorage.

关键快速示例:

 var keyName = window.localStorage.key(0);
Run Code Online (Sandbox Code Playgroud)

设置项目快速示例:

 window.localStorage.setItem("key", "value");
Run Code Online (Sandbox Code Playgroud)

获取项目快速示例

 var value = window.localStorage.getItem("key");
 // value is now equal to "value"
Run Code Online (Sandbox Code Playgroud)

删除项目快速示例:

 window.localStorage.removeItem("key");
Run Code Online (Sandbox Code Playgroud)

清除快速示例:

 window.localStorage.clear();
Run Code Online (Sandbox Code Playgroud)

如果您在移动设备和网络上使用javascript,则可以使用此代码来检测该环境:

var wl = window.location.href;
var mob = (wl.indexOf("android")>0);
Run Code Online (Sandbox Code Playgroud)

参考文献:http : //docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage http://cordova.apache.org/docs/en/6.x/cordova/storage/storage.html #页面TOC源

请注意:在iOS上使用匿名导航可能会使localstorage无法像spected那样工作.一个对我来说很好的简单测试:

$(document).ready(function () {
    try {
        localStorage.setItem('test', '1');
    } catch (Err) {
        if (Err.message.indexOf('QuotaExceededError') > -1) {
            // Tell the user they are in anonymous mode
            // Sugest it to go to https://support.apple.com/pt-br/HT203036 to get help to disable it
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • @shamaleyte"LocalStorage更像缓存而不是cookie,其中每个缓存的持久性取决于用户的浏览器设置,以及浏览器本身如何实现它(因为没有规范)"http://stackoverflow.com/questions/9948284 /如何持久,是-的localStorage (2认同)

Mik*_*e N 5

与您类似,我想在我的应用程序中使用服务器设置的 cookie,以便我的应用程序与网络保持一致,并且不需要单独的设备 ID 或其他身份验证方法。

我发现的是以下内容:

  • 通过 AJAX(例如 jQuery$.get()$.post())设置的 Cookie不会持续存在
  • 在“inAppBrowser”中设置的 Cookie确实存在。

使 cookie 持久化的方法是使用inAppBrowser插件。

首先,设置一个简单的服务器,它接受您想要保留的 GET 参数键值参数。我是一个 python/tornado 人,所以我的服务器可能看起来像:

class PersistCookieHandler(tornado.web.RequestHandler):
   @tornado.gen.coroutine
   def get(self):
      token = self.get_argument('token')
      self.set_secure_cookie('token',token)
Run Code Online (Sandbox Code Playgroud)

然后,在您的应用中:

function persistToken(token,success_cb, error_cb) {
    // replace with your URL
    url = 'SECURE_SERVER_URL_THAT_HANDLES_SET_COOKIE';  

    // _blank tells inAppBrowser to load in background (e.g., invisible)
    var ref = window.open(url, '_blank', 'location=no,toolbar=no');

    // attach a listener to the window which closes it when complete
    ref.addEventListener('loadstop', function(event) { 
        ref.close();
        success_cb();  // call our success callback
    });

    // attach a listener for server errors 
    ref.addEventListener('loaderror', function(event) { 
        // call our error callback
        error_cb(event);    
    });
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以按如下方式调用它:

persistToken(
   someToken,
   function() {
       console.log("token persisted");
   },
   function() {
       console.log("something went wrong);
   }
);
Run Code Online (Sandbox Code Playgroud)