.net AWS Cognito确认忘记密码无效请求

5f0d552i  于 2023-02-26  发布在  .NET
关注(0)|答案(1)|浏览(139)

我正在尝试使用C# for AWS Cognito调用ConfirmForgotPassword。当使用下面的代码时,我得到的只是一个异常“请求不包含有效参数”,没有其他信息。
有没有办法找出无效参数是什么?

using (var cognito = new AmazonCognitoIdentityProviderClient(AWS_AccessKey, AWS_SecretKey, AWSRegion))
{
    ConfirmForgotPasswordRequest confirmForgotPasswordRequest = new ConfirmForgotPasswordRequest();
    confirmForgotPasswordRequest.Username = userName;
    confirmForgotPasswordRequest.ClientId = clientId;
    confirmForgotPasswordRequest.Password = password;
    confirmForgotPasswordRequest.ConfirmationCode = confirmationCode;
    ConfirmForgotPasswordResponse confirmForgotPasswordResponse = new ConfirmForgotPasswordResponse();
    try
    {
        confirmForgotPasswordResponse = await cognito.ConfirmForgotPasswordAsync(confirmForgotPasswordRequest);
    }
    catch (Exception ex)
    {
      // Raises exception "Request does not contain valid parameters"
    }
    
}

我不确定secret hash参数是否是必需的,我已经在下面的代码中尝试过了,得到了相同的结果

confirmForgotPasswordRequest.SecretHash = HmacSha256(userName + clientId, clientSecret);

private string HmacSha256(string message, string secret)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] keyBytes = encoding.GetBytes(secret);
    byte[] messageBytes = encoding.GetBytes(message);
    System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);

    byte[] bytes = cryptographer.ComputeHash(messageBytes);

    return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
uurity8g

uurity8g1#

您不需要对代码进行哈希运算,您应该在调用ForgotPasswordAsync方法后,将通过电子邮件发送到用户确认的电子邮件地址的准确代码发送到用户的电子邮件地址。
通常InvalidParameter异常表示ClientId不正确,如果是代码或密码,则会得到a different type of exception
异常的Message属性说明了什么?如果是ClientId导致了问题,您可能会看到如下内容:

"2 validation errors detected: Value '' at 'clientId' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w+]+; Value '' at 'clientId' failed to satisfy constraint: Member must have length greater than or equal to 1"

相关问题