You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Cognito调用RespondToAuthChallenge报缺Session但文档标注参数可选

问题原因
  • AWS官方文档标注RespondToAuthChallenge接口的Session参数为可选,仅适用于自定义挑战首次触发等极少数特殊场景。你当前需要应答的SMS_MFA挑战属于常规认证流中的环节,Session为必填参数。
  • Session参数是Cognito服务在你上一步调用InitiateAuth完成用户名密码校验后,随SMS_MFA挑战标识一同返回的会话凭证,用来关联本次MFA验证对应的用户认证上下文,缺失后服务端无法识别你当前提交的验证码对应哪次认证请求,因此抛出必填参数缺失异常。
修复方案
  1. 保存上一步调用InitiateAuth(或前一次RespondToAuthChallenge)接口返回结果中的Session字段值
  2. 构造RespondToAuthChallengeRequest时将该值通过withSession方法传入即可,修改后的参考代码如下:
// 此处的previousAuthSession为上一步InitiateAuth返回的Session值
String previousAuthSession = initiateAuthResult.getSession();

Map<String, String> challengeResponses = new HashMap<String, String>();
challengeResponses.put("USERNAME", "username");
challengeResponses.put("SMS_MFA_CODE","code");
challengeResponses.put("SECRET_HASH", secretHash);

RespondToAuthChallengeRequest respondToAuthChallengeRequest = new RespondToAuthChallengeRequest()
        .withChallengeName(ChallengeNameType.SMS_MFA)
        .withClientId("client_id")
        .withChallengeResponses(challengeResponses)
        // 新增传入session参数
        .withSession(previousAuthSession);

RespondToAuthChallengeResult respondToAuthChallengeResult = cognitoClient.respondToAuthChallenge(respondToAuthChallengeRequest);

内容的提问来源于stack exchange,提问作者user2160919

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 17:45:03