【问题标题】:AWS Cognito RefreshToken API always show "SecretHash does not match for the client"AWS Cognito RefreshToken API 始终显示“SecretHash 与客户端不匹配”
【发布时间】:2022-12-22 12:36:27
【问题描述】:

我创建了一个 AWS Cognito 用户池并添加了一个带有密码的 APPClient。当我使用 DotNet SDK 进行注册、登录、cofirmSignup、注销时,这些 API 是成功的。但是,当我尝试通过刷新令牌刷新 accessToken 时,我总是遇到异常“SecretHash 与客户端不匹配:xxxxxx(App 客户端 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);

我也尝试了另一种方式,但得到了同样的异常:

    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); 

【问题讨论】:

    标签: .net amazon-web-services sdk amazon-cognito


    【解决方案1】:

    最后,我自己找到了。

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 2016-09-05
      • 1970-01-01
      • 2015-10-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多