【问题标题】:Retrieve IdentityId for a Cognito User检索 Cognito 用户的 IdentityId
【发布时间】:2018-04-01 11:05:57
【问题描述】:

首先,我已经阅读了类似的问题:

我们使用 Cognito 用户并通过存储桶策略为他们提供对存储桶中文件夹的访问权限 - 例如 BUCKET_NAME/user_data/eu-west-2:00000000-0000-0000-0000-000000000000。

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:ListBucket",
        "Resource": "arn:aws:s3:::BUCKET_NAME",
        "Condition": {
            "StringLike": {
                "s3:prefix": [
                    "user_data/${cognito-identity.amazonaws.com:sub}/",
                ]
            }
        }
    },
    {
        "Effect": "Allow",
        "Principal": "*",
        "Action": ["s3:GetObject", "s3:PutObject"],
        "Resource": [
            "arn:aws:s3:::BUCKET_NAME/user_data/${cognito-identity.amazonaws.com:sub}/*"
        ]
    }
]
}

这非常适合使用 javascript API 的客户端。他们进行身份验证,我可以使用 AWS.config.credentials.identityId1 访问 S3。

但是,如果我想从我的 .Net Web API 后端访问用户的存储桶,我找不到获取 IdentityId 的方法。所以我不知道哪个文件夹是用户的,这违背了能够创建用户文件夹的整个目的。例如,当用户注册时,我可能想创建包含现有项目的文件夹...

一种解决方法是让客户端发布 IdentityId,以便它可以针对用户子存储,但是假设 - 1.它发生了; 2.身份证有效。

TLDR:如何使用 AWSSDK.CognitoIdentity 从 Cognito 用户名(子)中获取 Cognito IdentityId?

1 重要的一点 - 正如我在很长一段时间后发现的那样,${cognito-identity.amazonaws.com:sub} 不是用户子。文档非常不清楚,以至于我不知道 ${cognito-identity.amazonaws.com:sub} 会是什么样子。

【问题讨论】:

    标签: .net amazon-web-services authentication amazon-s3 amazon-cognito


    【解决方案1】:

    这实际上可能会让您有一个良好的开端:

    Amazon.CognitoIdentity.CognitoAWSCredentials creds = 
        new Amazon.CognitoIdentity.CognitoAWSCredentials("pool id", Amazon.RegionEndpoint.USEast1, ...);
    creds.GetIdentityId();
    

    这里有一个线索:http://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html 在 Unity 部分中。

    nuget:package id="AWSSDK.CognitoIdentity" version="3.3.2.16"

    希望对您有所帮助! ...他们的文档对特定用例没有多大帮助,所以我们必须团结一致!

    【讨论】:

    • 是的,只要我有用户密码就可以工作,这样我就可以作为他们进行身份验证并将 ID 令牌传递给 GetIdentityId。我曾希望我不必代理身份验证过程......
    • 我可以看到一些一直获取密码的方法是有问题的 - 无会话,用户池作为 SSO。您是否使用 Lambda 来创建我想知道的文件夹?
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 2016-04-09
    • 2016-12-13
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多