此问题可能是由于 AWS Cognito 服务会在忘记密码时默认将用户的密码重置为随机密码,而不是通过触发 UserMigration_ForgotPassword 方法来进行密码重置。如果需要使用 UserMigration_ForgotPassword 方法,可以通过在 AWS Cognito 管理控制台中进行以下设置来禁用该默认行为:
这样设置后,在忘记密码时将会触发 UserMigration_ForgotPassword 方法。下面是一个示例 Lambda 函数,可供参考:
import boto3
import json
def lambda_handler(event, context):
print("Received event:", json.dumps(event, indent=2))
# Retrieve values from event
username = event['userName']
new_password = event['request']['newPassword']
# Call method to reset password for the user using username and new password
# Here you can use your custom code to update user's password
client = boto3.client('cognito-idp')
response = client.admin_set_user_password(
UserPoolId='',
Username=username,
Password=new_password,
Permanent=True
)
print("Response from admin_set_user_password:", json.dumps(response, indent=2))
return event