如何通过AWS Node.js API为创建的Cognito用户池启用Authenticator MFA?
解决AWS Cognito用户池启用软件令牌MFA的参数错误问题
问题原因
你遇到的UnexpectedParameter错误,是因为AWS Node.js SDK v2的createUserPool API中不存在直接的SoftwareTokenMfaConfiguration参数。该参数属于后续更新用户池MFA配置的setUserPoolMfaConfig接口,创建用户池时启用软件令牌MFA需要使用正确的参数结构。
解决方案
修改createUserPool的参数,通过EnabledMfas数组指定允许的MFA方式,同时保持MfaConfiguration设置为"ON"(强制MFA)或"OPTIONAL"(可选MFA)。
修正后的代码片段
async function createCognitoUserPool(poolName) { const cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider(); // Define the parameters for creating the user pool const params = { PoolName: poolName, Policies: { PasswordPolicy: { MinimumLength: 8, RequireUppercase: true, RequireLowercase: true, RequireNumbers: true, RequireSymbols: true, }, }, MfaConfiguration: "ON", // 强制启用MFA EnabledMfas: ["SOFTWARE_TOKEN_MFA"], // 指定使用软件令牌作为MFA方式 AutoVerifiedAttributes: ["phone_number"], SmsConfiguration: JSON.parse( process.env.COGNITO_USER_POOL_SMS_CONFIGURATION ), }; try { const response = await cognitoIdentityServiceProvider .createUserPool(params) .promise(); console.log("User Pool created successfully:", response.UserPool.Id); return response.UserPool.Id; } catch (error) { console.error("Error creating User Pool:", error); throw error; } }
补充说明
- 若需同时支持短信MFA和软件令牌MFA,将
EnabledMfas设置为["SOFTWARE_TOKEN_MFA", "SMS_MFA"] - 若使用AWS SDK v3,
createUserPoolAPI支持直接传入驼峰式的softwareTokenMfaConfiguration参数,结构如下:
同时仍需设置softwareTokenMfaConfiguration: { enabled: true }mfaConfiguration为"ON"或"OPTIONAL"
内容的提问来源于stack exchange,提问作者Mohan
相关产品推荐
相关产品推荐

