如何从aws cognito-android获取密码?

如何从aws cognito-android获取密码?,android,amazon-web-services,aws-lambda,aws-sdk,aws-cognito,Android,Amazon Web Services,Aws Lambda,Aws Sdk,Aws Cognito,所以我正在使用aws cognito,我对如何获取密码有点困惑 如果用户在编辑文本中输入密码,如何获取用户注册时输入的密码,以便我可以比较他们登录时使用的密码和注册时使用的密码 以下是我必须注册用户的代码: userPool.signUpInBackground(username_ET.getText().toString(), password_ET.getText().toString(), userAttributes, null, signupCallback); 下面是我用来登录的代

所以我正在使用aws cognito,我对如何获取密码有点困惑

如果用户在编辑文本中输入密码,如何获取用户注册时输入的密码,以便我可以比较他们登录时使用的密码和注册时使用的密码

以下是我必须注册用户的代码:

userPool.signUpInBackground(username_ET.getText().toString(), password_ET.getText().toString(), userAttributes, null, signupCallback);
下面是我用来登录的代码:

 private AuthenticationHandler authenticationHandler = new AuthenticationHandler()
    {
        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice)
        {
            Log.d(COGNITO_LOGIN,"Login success I think?!");
            cognitoUser.getDetailsInBackground(getDetailsHandler);
            //Here i need to compare passwords before i can move on.
        }
        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId)
        {
            Log.d(COGNITO_LOGIN,passwordET.getText().toString());
            // The API needs user sign-in credentials to continue
            AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, passwordET.getText().toString(), null);

            // Pass the user sign-in credentials to the continuation
            authenticationContinuation.setAuthenticationDetails(authenticationDetails);

            // Allow the sign-in to continue
            authenticationContinuation.continueTask();
        }
        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) {
            // Multi-factor authentication is required; get the verification code from user
            multiFactorAuthenticationContinuation.setMfaCode("verificationCode");
            // Allow the sign-in process to continue
            multiFactorAuthenticationContinuation.continueTask();
        }
        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
        }
        @Override
        public void onFailure(Exception exception)
        {
            // Sign-in failed, check exception for the cause
            Log.d(COGNITO_LOGIN,"Login failed!");
            Log.d(COGNITO_LOGIN,exception.getMessage());
            exceptionMessage(exception.getMessage());
        }
    };



cognitoUser.getSessionInBackground(authenticationHandler);

使用身份验证处理程序,我只需要传入正确的用户名(或userID)即可运行onSuccess。甚至不需要密码。所以我很困惑,用户必须输入正确的密码才能登录

您不需要比较密码。当您注册时,Cognito会为您注册的密码存储一个salt和一个验证器。Cognito不会以您输入的形式存储您的密码,而只存储salt和验证器。当您使用下面的代码时,Cognito使用安全远程密码协议来匹配内部存储的验证器。由于我们使用您为计算提供的密码,因此您无法检索该密码。请注意,在onSuccess回调中,如果调用成功,您将获得令牌,如下所述

// Callback handler for the sign-in process 
AuthenticationHandler authenticationHandler = new AuthenticationHandler() { 

    @Override 
    public void onSuccess(CognitoUserSession cognitoUserSession) { 
        // Sign-in was successful, cognitoUserSession will contain tokens for the user   
    }

    @Override
    public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) { 
        // The API needs user sign-in credentials to continue
        AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);

        // Pass the user sign-in credentials to the continuation
        authenticationContinuation.setAuthenticationDetails(authenticationDetails);

        // Allow the sign-in to continue
        authenticationContinuation.continueTask();
    }

    @Override
    public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) { 
        // Multi-factor authentication is required, get the verification code from user
        multiFactorAuthenticationContinuation.setMfaCode(mfaVerificationCode);
        // Allow the sign-in process to continue
        multiFactorAuthenticationContinuation.continueTask();
    }

    @Override
    public void onFailure(Exception exception) {
        // Sign-in failed, check exception for the cause
    } 
};

// Sign-in the user 
cognitoUser.getSessionInBackground(authenticationHandler);

我不确定我是否理解。您在刚刚发布的代码中提供了密码。创建AuthenticationDetails并将其添加到延续时。那么你的问题到底是什么?@doorstack是的,我提供了密码,但是
getAuthenticationDetails
中的log语句没有运行。当我输入错误的密码时,它无论如何都会登录,尝试呼叫注销或重新安装应用程序。然后它没有存储凭据,它将使用
getAuthenticationDetails
方法请求密码。感谢您的回复。我很感激。我还有一个问题。当用户忘记密码时,您将如何处理我知道存在的:
user.forgotPasswordInBackground(新ForgotPasswordHandler…
。但是,我认为它应该以某种方式向我发送另一个验证代码,我如何获得该代码。我在这里发布了这个问题的详细版本:是的,如果您在调用忘记密码时有一个经过验证的电子邮件或电话号码,您应该会得到一个代码。一旦用户登录,即使传递的密码不正确,getSession也会成功。如果在应用程序内部,我们需要验证用户是否允许更改关键内容。如何检查输入的密码是否正确?