【发布时间】: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