【发布时间】:2018-09-18 05:25:06
【问题描述】:
在一个 android 应用程序中,一旦用户完成对 Cognito 用户池的身份验证,我就会收到来自 http://<domain>.auth.<region>.amazoncognito.com/login 的 JWT access_token。该用户池链接到 Cognito 身份池。
我应该使用该 access_token 调用什么 API 来获取 AWSCredentials 对象。
我找到的最接近的是AssumeRoleWithWebIdentity,但那是一个 STS API,我在网上阅读的一些内容似乎建议开发人员不要直接使用 STS,而是依赖 Cognito。
此外,我不希望我需要的 API 需要指定角色名称。 Cognito 身份池已配置为为经过身份验证的用户提供特定角色。 AssumeRoleWithWebIdentity 将角色名称作为 API 的输入。因此,这看起来不对。
我查看了Cognito Identity Pool API Reference,但找不到接受 access_token 并返回 AWS 凭证的 API。
更新:
以下使用GetCredentialsForIdentity 的答案抛出ResourceNotFoundException 说它找不到指定的IdentityId。
string access_token = ...
var jwtAccessToken = System.IdentityModel.Tokens.Jwt.JwtSecurityToken(access_token);
var client = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(),REGION);
var response = await client.GetCredentialsForIdentityAsync(new GetCredentialsForIdentityRequest
{
IdentityId=String.Format("{0}:{1}", REGION, jwtAccessToken.id),
Logins=new Dictionary<string,string>
{
{String.Format("cognito-idp.{0}.amazonaws.com/{1}", REGION, USER_POOL_ID),
access_token}
}
});
【问题讨论】:
标签: amazon-web-services amazon-cognito aws-cognito