【问题标题】:Create user with custom attribute using AdminCreateUser in AWS Cognito在 AWS Cognito 中使用 AdminCreateUser 创建具有自定义属性的用户
【发布时间】:2018-11-02 08:01:30
【问题描述】:

我正在尝试使用以下代码在 AWS Cognito 中使用 adminCreateUser API 创建用户

var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();

var params = {
      UserPoolId: "us-east-1_302HlhnaC", /* required */
      Username : "test@yopmail.com",
      ForceAliasCreation: true,
      TemporaryPassword: 'test@yopmail.com',
      UserAttributes: [
        {
          Name: 'given_name', /* required */
          Value: 'test'
        },
        {
          Name: 'family_name', /* required */
          Value: 'kumar'
        },
        {
          Name: 'name', /* required */
          Value: 'test'
        },
        {
          Name: 'custom:dob', /* required */
          Value: '1990-07-25'
        },
        {
          Name: 'email', /* required */
          Value: 'test@yopmail.com',
        },
        {
          Name: 'email_verified', /* required */
          Value: 'true',
        }
        /* more items */
      ],

};


cognitoidentityserviceprovider.adminCreateUser(params, function(error, data) {
    console.log(error,data);
    res.send("test");
});

它总是抛出以下异常: InvalidParameterException:属性不符合架构:自定义:dob:架构中不存在属性。

我做错了什么,如果是,请告诉我解决方案。

谢谢

【问题讨论】:

    标签: amazon-web-services amazon-cognito


    【解决方案1】:

    您必须提前添加自定义属性。您可以通过访问用户池并单击Attributes 链接来创建自定义属性。

    【讨论】:

    • 所以我在 IAM 管理控制台上,没有看到 UserPool 选项。
    【解决方案2】:

    在这里添加我的案例。

    在我的 CloudFormation 中,我有:

      Schema:
        - AttributeDataType: String
          Name: role
          DeveloperOnlyAttribute: true
          Mutable: true
          Required: false
    

    在控制台中,它被翻译成:

    在应用程序adminCreateUser 调用中,我必须将其提供为dev:custom:role

      cognitoService.adminCreateUser({
        UserPoolId: config.cognitoUserPoolId,
        Username: email,
        UserAttributes: [{
          Name: 'dev:custom:role',
          Value: role,
        }]
      }).promise()
    

    通过尝试想通了。希望我知道这方面的文档在哪里。

    【讨论】:

    • 非常感谢您!这方面有 0 个文档。 ? 将 'custom:dev:custom:role' 更改为 'dev:custom:role' 使其工作......
    • 是的,这对他们来说是糟糕的文档。我浪费了 2 个小时试图弄清楚为什么它不起作用。感谢您的详尽回答
    • 我的问题还在于自定义属性需要 custom: 前缀,但未记录在案,谢谢
    【解决方案3】:

    达西的回答是正确的。但我想详细说明,因为该答案集中在 AWS Web 控制台上。

    还有另一个答案,即前缀“dev:”可能是一个未记录的解决方法(因此没有文档),并且可能会在没有警告的情况下停止工作。

    首先,必须在创建用户池时创建自定义属性。

            CreateUserPoolRequest request = new CreateUserPoolRequest
            {
               ...
    
                Schema = new List<SchemaAttributeType>
                {
                new SchemaAttributeType
                {
                    Name = "email",
                    AttributeDataType = AttributeDataType.String,
                    Required = true,
                    Mutable = false
                },
                new SchemaAttributeType //custom attribute
                {
                    Name = "blah",
                    AttributeDataType = AttributeDataType.String,
                    Mutable = false
                },
    
                ...
            };
    

    然后在创建用户的时候,就可以设置了。

            var request = new AdminCreateUserRequest
            {
                 ...
    
                UserAttributes = new List<AttributeType>
                {
                    new AttributeType
                    {
                        Name = "email",
                        Value = "xyz@xyz.com"
                    },
                    new AttributeType //custom attribute
                    {
                        Name = $"custom:blah",
                        Value = "value for blah"
                    }
                }
            };
    

    现在,只需添加“custom:”前缀即可。

    另请注意,AWS 延续了其 API 不一致的传统,在创建用户池时不必添加前缀,而在创建用户时使用前缀。

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 2017-09-08
      • 2018-03-25
      • 2021-03-29
      • 1970-01-01
      • 2017-03-21
      • 2018-03-03
      • 1970-01-01
      • 2021-02-10
      相关资源
      最近更新 更多