简体   繁体   English

AWS Cognito:如何以编程方式删除身份?

[英]AWS Cognito: how to delete an identity programmatically?

In my Android app, I use AWS Cognito to allow my users to sign up and login. 在我的Android应用程序中,我使用AWS Cognito允许我的用户注册和登录。 I configured my identity pool so it allows guest users (unauthenticated users) in my app. 我配置了身份池,以便它允许我的应用程序中的访客用户(未经身份验证的用户)。

So everytime my user logs in or logs out, I call the following code to get an identity ID: 因此,每当我的用户登录或注销时,我都会调用以下代码来获取身份ID:

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
    this,
    "eu-central-1:12345678-1234-5678-9012-1234567890ab",
    "eu-central-1");
credentialsProvider.clearCredentials();
credentialsProvider.getIdentityId();

When my user logs in, it creates a new identity the first time, then the user gets the same identity everytime he logs in. But everytime he logs out, a new identity is created, since he's now a "guest" user. 当我的用户登录时,它将首次创建一个新的身份,然后该用户每次登录都会获得相同的身份。但是,每次他注销时,都会创建一个新的身份,因为他现在是“来宾”用户。

Is that the expected behavior? 这是预期的行为吗? And if yes, how can I remove an unused identity programmatically (for example when the user logs in, so the "guest" identity is now useless, since a new one will be created when he logs out)? 如果是的话,我该如何以编程方式删除未使用的身份(例如,当用户登录时,“来宾”身份现在已无用,因为在用户注销时会创建一个新的身份)?

Thanks for your help. 谢谢你的帮助。

From the docs, we have: 从文档中,我们有:

getIdentityId() Gets the Cognito identity id of the user. getIdentityId()获取用户的Cognito身份ID。 The first time when this method is called, a network request will be made to retrieve a new identity id. 首次调用此方法时,将发出网络请求以检索新的身份ID。 After that it's saved in SharedPreferences. 之后,将其保存在SharedPreferences中。

clearCredentials() Destroy all the saved AWS credentials but not the identity Id. clearCredentials()销毁所有已保存的AWS凭证,但不破坏身份ID。

so, the behavior you described is totally normal according to the docs. 因此,根据文档,您描述的行为是完全正常的。

Now, let's find an approach to deal with the problem. 现在,让我们找到一种解决问题的方法。

if you want to continue with your proposition, and delete the identity programmatically after the logout, you can use deleteUser() method from AWSJavaSDK. 如果要继续您的主张,并在注销后以编程方式删除身份,则可以使用deleteUser()方法。

However, if you want to keep the same identity for the same user after the logout, you shouldn't clear the cache, cause the won't keep the user any trace of its identity. 但是,如果要在注销后为同一用户保留相同的身份,则不应清除缓存,因为这将不会使用户保留其身份的任何痕迹。

I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM