如何保存第一次登录的 sessionId 并且不会让我每次都在 inappwebview 中登录

Posted

技术标签:

【中文标题】如何保存第一次登录的 sessionId 并且不会让我每次都在 inappwebview 中登录【英文标题】:How to save sessionId of first login and doesn't make me login every time in inappwebview flutter 【发布时间】:2021-05-17 13:40:42 【问题描述】:

我想编写一个应用程序,让我从应用程序注册并发送发布请求并获得对 inappwebview 的响应,就像下面的考试一样

InAppWebView(
  initialUrl: widget.initUrls,
  initialOptions: InAppWebViewGroupOptions(
  crossPlatform: InAppWebViewOptions(
      mediaPlaybackRequiresUserGesture: false,
      debuggingEnabled: true,
    ),
  ),
  onWebViewCreated: (InAppWebViewController c)
    _controller = c;
    if(staticEmail != null && staticPass != null) 
      _controller.postUrl(url: "https://xxxxxxx.com/my-account", postData: utf8.encode(
          "email=$staticEmail&password=$staticPass&wooc_user_phone=$staticShop&wooc_user_name=$staticShop&woocommerce-register-nonce=5d1b626841&_wp_http_referer=/my-account/&register=Register") )
          .whenComplete(() => print("done")).catchError((err)print("err : $err");
            isloading = false;
          );
    
  ,
);

它登录正常。

但是当我转到另一个链接或当我关闭应用程序并再次登录时,它要求我再次登录我只想保存不会让我每次都登录的 cookie 或会话 ID

【问题讨论】:

【参考方案1】:

您可以使用这个flutter_inappwebview 包的CookieManager 来获取和设置cookies。这是插件使用的单例,您可以通过以下方式访问它:

CookieManager.instance()

【讨论】:

你能解释一下如何在我的代码中使用它 基于您提供的小代码 sn-p 这真的很难。我建议您在关闭和/或打开应用内 web 视图时检查 CookieManager.instance().getCookies() 的内容,以找出最适合您的用例的内容。 我尝试使用 cookieManager 并将 cookie 保存在 sharedpreference 中并调用它 onwebviewcreated 但也不起作用

以上是关于如何保存第一次登录的 sessionId 并且不会让我每次都在 inappwebview 中登录的主要内容,如果未能解决你的问题,请参考以下文章

Session.Abandon 调用后 SessionID 仍然相同

分布式系统session一致性问题

HttpSession详解

sessioncookietoken工作原理及区别

SpringBoot29 登录逻辑登录状态判断

ASP.NET做的网站如何准确的限制登录的用户数