当错误消息显示用户名和密码不匹配时,您如何通过 Cognito 对用户池中的用户进行身份验证?

How do you authenticate a user in a user pool via Cognito when error messages say userName and password don't match even though they do?

所以我在 Javascript 中摆弄 Cognito 及其 Beta 用户池功能。多亏了这里的文档,我成功地创建了用户:

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

当我尝试对用户进行身份验证时,我收到一条奇怪的错误消息:

NotAuthorizedException: 用户名或密码不正确。

我不明白为什么会这样,因为我实际上是在复制和粘贴我用来创建用户的用户名和密码的值,但效果很好。

下面是我验证用户的代码:

//----- Setup
AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myIdentityPoolId'
    });

    AWSCognito.config.region = 'us-east-1';

    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myIdentityPoolId'
    });

    var poolData = { UserPoolId : 'myUserPoolId',
        ClientId : 'myClientId'
    };

//-----Authenticate a user
    alert('Authenticate a user with application');
    var authenticationData = {
        Username : 'markie17061993',
        Password : 'MeowWoof123456789!',
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'myUserPoolId',
        ClientId : 'myClientId'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : 'markie17061993',
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    alert('hiss');
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
        },

        onFailure: function(err) {
            alert(err);
        },

    });

你看这里有什么问题吗?我想也许我在这两个地方的 UserName 参数是不同的值或其他东西:

var authenticationData = {
        Username : 'markie17061993',
        Password : 'MeowWoof123456789!',
    };

var userData = {
        Username : 'markie17061993',
        Pool : userPool
    };

谢谢

如果您确认新注册的用户不是,则不会显示您的代码。如果不是,那将解释 NotAuthorizedExceptionThis example具体就是你要关注的。

如果您不想确认正在从您的应用注册的用户,您应该使用 PreSignUp triggerautoConfirm 您的用户。