AWS Cognito:Cognito ID 和 sub 之间的区别,我应该使用什么作为主键?

AWS Cognito: Difference between Cognito ID and sub, what should I use as primary key?

我正在使用 AWS Cognito 构建无服务器后端以进行用户管理。

Cognito 同时使用 cognitoIdsub 来识别用户。

来自官方 awslabs 的

This 项目使用 cognitoId 作为数据库表中的主键 link 数据到用户对象,但是关于 sub 的文档明确指出:

sub: the UUID of the authenticated user. This is not the same as username.

问题:我应该使用什么作为主键,cognitoIDsub

命名可能会让人混淆,我会尽力澄清。

Amazon Cognito 下通常有两个池:

  • 用户池
  • 身份池(联合身份)

您所指的 "sub" 在 IAM 策略中通常表示为

${cognito-identity.amazonaws.com:sub}

并将解析为(在 javascript sdk 中)

中找到的值

AWS.config.credentials.identityId

看起来像

us-east-1:########-####-####-####-############

它只会在凭据刷新后存在于凭据上。

所以为了回答你的问题,

  • sub(subject) 是全局唯一的,因此对于用户池也是唯一的。
  • 与可以重新分配给用户池中的其他用户的用户名不同,sub 永远不会重新分配。

Source