我想知道在移动应用程序上存储 key 以进行 session 身份验证的机制。我有一个 Tornado 网络服务器,它将使用第三方外部服务来验证用户身份,例如 Facebook 或谷歌。我熟悉在使用浏览器时使用 set_secure_cookie
存储 cookie。但是,如果移动应用程序现在正在连接并进行身份验证,该怎么办?我将使用什么机制来存储 secret (例如安全 cookie)以供将来的 session 身份验证?下面显示了验证用户的代码:
class GoogleOAuth2LoginHandler(tornado.web.RequestHandler,
tornado.auth.GoogleOAuth2Mixin):
async def get(self):
if self.get_argument('code', False):
user = await self.get_authenticated_user(
redirect_uri='http://your.site.com/auth/google',
code=self.get_argument('code'))
# Save the user with e.g. set_secure_cookie
else:
await self.authorize_redirect(
redirect_uri='http://your.site.com/auth/google',
client_id=self.settings['google_oauth']['key'],
scope=['profile', 'email'],
response_type='code',
extra_params={'approval_prompt': 'auto'})
对于不依赖浏览器和 cookie 支持的移动应用程序,如何对此进行修改?
最佳答案
在 iOS 中,NSHTTPCookie 类中有一个 API,您可以在其中保存整个 http 响应字符串。 创建 cookie 的代码如下所示。
if let requestUrl = url {
let httpCookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields as! [String : String], for: requestUrl)
}
然后就可以保存cookie了,
HTTPCookieStorage.shared.setCookies(httpCookie, for: url, mainDocumentURL: url)
如果需要,您还可以访问此 cookie 并将其设置到 WebView。
关于android - 相当于在没有浏览器的移动应用程序上存储cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57968568/