AWS认知新密码延续- Android

yxyvkwin  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(156)

我已经尝试了好几天,希望有人能帮助我。我正在构建一个Android应用程序,并集成Amazon Cognito登录。我想使用管理面板在Amazon Cognito中创建管理员用户。这样做时,一个要求是用户更改密码。在CognitoUserPoolSignInProvider中,这是一个匿名类,为了使用新密码对用户进行身份验证,我在anonymous类中使用了以下代码:

@Override
    public void authenticationChallenge(final ChallengeContinuation continuation) {

        if ("NEW_PASSWORD_REQUIRED".equals(continuation.getChallengeName())) {
            NewPasswordContinuation newPasswordContinuation = (NewPasswordContinuation) continuation;
            newPasswordContinuation.setPassword("users new password goes here");
            continuation.continueTask();
        }

    }

我有一个单独的Activity类ChangePassword,它链接到一个用户界面,并在编辑文本框中获取用户的输入。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        password = (EditText) findViewById(R.id.newPassword);
        submit = (Button) findViewById(R.id.submit);

        String pass = password.getText().toString();

如何让用户输入到匿名类中来设置新密码?如有任何帮助,我们将不胜感激

hrirmatl

hrirmatl1#

您需要使用按钮单击回调来获取用户密码。由于您的代码现在已经编写完成,密码将被设置为空字符串(或者创建时EditText字段中的任何内容)。
从这个开始:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_password);
    final EditText password = (EditText) findViewById(R.id.newPassword);
    Button submit = (Button) findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String pass = password.getText().toString();
        }
    });
}

设置按钮点击动作后,创建一个类示例来覆盖authenticationChallenge方法,然后将该类传递给相应的AWS类进行身份验证,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_password);
    final EditText password = (EditText) findViewById(R.id.newPassword);
    Button submit = (Button) findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String pass = password.getText().toString();
            AuthenticationHandler h = new AuthenticationHandler() {
                @Override
                public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice cognitoDevice) { }
                @Override
                public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String s) { }
                @Override
                public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) { }
                @Override
                public void authenticationChallenge(ChallengeContinuation continuation) {
                    if ("NEW_PASSWORD_REQUIRED".equals(continuation.getChallengeName())) {
                        NewPasswordContinuation newPasswordContinuation = (NewPasswordContinuation) continuation;
                        newPasswordContinuation.setPassword(pass);
                        continuation.continueTask();
                    }
                }
                @Override
                public void onFailure(Exception e) { }
            };
            CognitoUserPool pool = new CognitoUserPool(getApplicationContext(), "poolId", "clientId", "clientSecret", Regions.US_WEST_2);
            pool.getUser("userId").getSession(h);
        }
    });
}

相关问题