AWS Cognito RefreshToken API 始终显示“SecretHash 与客户端不匹配”

Law*_*ule 8 .net sdk amazon-web-services amazon-cognito

我创建了一个 AWS Cognito 用户池并添加了一个带有秘密的 APPClient。当我使用DotNet SDK进行注册、登录、cofirmSignup、注销时,这些API都是成功的。但是,当我尝试通过刷新令牌刷新 accessToken 时,我总是遇到异常“SecretHash 与客户端不匹配:xxxxxx(应用程序客户端 ID)”。有人可以帮忙吗?我的代码如下:

    var userPool = new CognitoUserPool(_cognitoSecret.CognitoUserPoolId, _cognitoSecret.CognitoAppClientId, _awsCognitoClient, appClientSecret);
    var cognitoUser = new CognitoUser(request.Username,
        _cognitoSecret.CognitoAppClientId, userPool, _awsCognitoClient, appClientSecret);

    cognitoUser.SessionTokens = new CognitoUserSession(null, null, request.RefreshToken, DateTime.UtcNow, DateTime.UtcNow.AddSeconds(Constants.DefaultTokenExpirationTime));

    var authRequest = new InitiateRefreshTokenAuthRequest
    {

        AuthFlowType = AuthFlowType.REFRESH_TOKEN_AUTH    // to refresh access token and id token
    };

    var response = await cognitoUser.StartWithRefreshTokenAuthAsync(authRequest);
Run Code Online (Sandbox Code Playgroud)

我也尝试了另一种方法,但得到了同样的例外:

    var refreshTokenRequest = new InitiateAuthRequest
    {
        ClientId = _cognitoSecret.CognitoAppClientId,
        AuthFlow = AuthFlowType.REFRESH_TOKEN_AUTH
    };

    refreshTokenRequest.AuthParameters.Add("REFRESH_TOKEN", request.RefreshToken);
    if (!string.IsNullOrWhiteSpace(_cognitoSecret.CognitoAppClientId) && !string.IsNullOrWhiteSpace(_cognitoSecret.CognitoAppClientSecret))
    {
        var secretHash = SecretHashComputation.GetSecretHash(request.Username,
            _cognitoSecret.CognitoAppClientId, _cognitoSecret.CognitoAppClientSecret);

        refreshTokenRequest.AuthParameters.Add("SECRET_HASH", secretHash);
    }

    var response = await _awsCognitoClient.InitiateAuthAsync(refreshTokenRequest); 
Run Code Online (Sandbox Code Playgroud)

Law*_*ule 21

最后我自己找到了。

为了AuthFlowType.REFRESH_TOKEN_AUTHSECRET_HASH必须由 Cognito 用户池中的用户名(子)计算,而不是电子邮件(如果我在创建用户池时选择电子邮件作为用户名)。

这很令人困惑,因为SECRET_HASH必须通过 other 中的电子邮件来计算AuthFlowType