我使用的是最新版本的AWS SDK for Android。
implementation 'com.amazonaws:aws-android-sdk-core:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.7.6'我的身份验证处理程序大部分取自他们的示例代码。
// create a handler for the sign-in process
private AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
// String idToken = userSession.getIdToken().getJWTToken();
// Map<String, String> logins = new HashMap<>();
// logins.put("cognito-idp.us-east-1.amazonaws.com/" + getString(R.string.user_pool_id), idToken);
// AuthHelper.getInstance().getCredentialsProvider().setLogins(logins);
//
// new RefreshCognitoCredentials().execute();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
String password = inputPassword.getText().toString();
AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);
authenticationContinuation.setAuthenticationDetails(authenticationDetails);
authenticationContinuation.continueTask();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
}
@Override
public void onFailure(Exception exception) {
String error = AuthHelper.formatException(exception);
layoutUsername.setErrorEnabled(true);
layoutUsername.setError(error);
}
};身份验证工作正常。它会像它应该做的那样进行缓存。在我的闪屏活动中,我能够检查CognitoUser.getCurrentUser().getUserId()。
现在要注销了:
CognitoUser.getCurrentUser().signOut()现在,如果我关闭应用程序并打开应用程序- CognitoUser.getCurrentUser().getUserId()仍然返回我之前登录的用户。
几个月前,我用2.2.+作为我的软件开发工具包版本,做了一个亚马逊网络服务的实现,这个例子像预期的那样工作。
注意*如果我尝试CognitoUser.getCurrentUser().globalSignout() -它返回一个'user is not authenticated‘错误。
如果我有一个有效的用户/会话,我如何检查应用程序启动?我讨厌AWS每天在没有文档或文档的情况下改变事情,这些文档是无法找到的。
发布于 2018-11-03 15:52:27
signOut从SharedPreferences中清除缓存的令牌。它清除存储在访问、id和刷新令牌下的数据。但是,signOut不会清除LastAuthUser密钥中包含的用户id。
当您调用cognitoUserPool.getCurrentUser().getUserId()时,它会检查SharedPreferences中是否存在LastAuthUser密钥,因此它会返回userId。我正在调查这个问题。当我确认预期的行为时,我将更新此答案。
发布于 2020-01-09 09:42:45
我在更新用户的电子邮件时遇到了这种情况。在我的例子中,当用户切换回以前使用的电子邮件时,cognito不会更新最后一个认证用户的值。
我的解决方案是手动更新共享首选项中的值,其中cognito更新最后一个经过身份验证的用户:
SharedPreferences.Editor editor = context.getSharedPreferences("CognitoIdentityProviderCache", 0).edit();
String csiLastUserKey = "CognitoIdentityProvider." + cognitoUserPool.getClientId() + ".LastAuthUser";
editor.putString(csiLastUserKey, newEmail);
editor.commit();这是一个老生常谈的变通方法,因为我是在强制自动完成cognito应该做的事情。
发布于 2019-06-11 23:48:01
这似乎是AWS SDK中的一个错误。
我几乎也有同样的问题。我的步骤:
1. Sign in as user 1
2. Sing out
3. Sign in as user 2
4. Surprise that user 1 is signed in instead of user 2因此,决定升级到新的SDK
com.amazonaws:aws-android-sdk-mobile-client:2.13.4
它使用
implementation 'com.amazonaws:aws-android-sdk-core:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.13.4'但这无济于事。
在调查中发现,
与/data/data/applicationId/shared_prefs/CognitoIdentityProviderCache.xml上的文件CognitoIdentityProviderCache.xml相关的问题
注销后未清除CognitoIdentityProviderCache.xml文件。
注销后的文件内容示例:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted">...</string>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.iv">...</string>
<string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.keyvaluestoreversion">1</string>
</map>从文件中删除这3行可以解决此问题。
解决方法:
注销时清除
但文件名可以在下一个版本中更改。
等同于在系统设置->应用程序-> AwesomeApp中按“清除数据”
context.cacheDir.parentFile.deleteRecursively()你可以关注我的错误报告,在他们的错误跟踪器上获取详细信息
https://stackoverflow.com/questions/53121639
复制相似问题