【问题标题】:Amazon Cognito "A client attempted to write unauthorized attribute"Amazon Cognito“客户端试图写入未经授权的属性”
【发布时间】:2017-10-16 06:28:23
【问题描述】:

我正在使用适用于 AWS Cognito 的 JavaScript 开发工具包,但有几个自定义属性我似乎无法保存,也看不出原因。

问题属性是可变字符串字段如下:

custom: role
custom: recruitingrole
custom: title

同一请求中的其他自定义字段似乎更新正常。具体来说,这些似乎有效:

custom:division
custom:linkedin
custom:location
custom:bio

当我通过 SDK 提交时,返回:

{"__type":"NotAuthorizedException","message":"客户端试图写入未经授权的属性"}

这是发送的数据,如 Chrome 开发者控制台网络输出所示:

{
    "AccessToken": "",
    "UserAttributes": [{
        "Name": "name",
        "Value": "Steve Austin"
    }, {
        "Name": "custom:company",
        "Value": "OSI"
    }, {
        "Name": "custom:division",
        "Value": "Bionics"
    }, {
        "Name": "custom:recruitingrole",
        "Value": "other"
    }, {
        "Name": "custom:linkedin",
        "Value": "http://www.linkedin.com"
    }, {
        "Name": "custom:location",
        "Value": "Mexico City, Mexico City, Mexico"
    }, {
        "Name": "custom:bio",
        "Value": "A man barely alive."
    }]
}

谁能建议我为什么不能保存到这些属性?

谢谢

【问题讨论】:

    标签: javascript amazon-web-services amazon-cognito


    【解决方案1】:

    当然,当我在 StackOverflow 上发帖后,答案就很清楚了。

    问题是我没有在与用户池关联的应用程序中为这些属性设置权限。文档应在讨论自定义属性的地方明确说明此要求。

    【讨论】:

    • 很高兴看到您找到了问题的答案。此功能的文档在这里:docs.aws.amazon.com/cognito/latest/developerguide/… 我可以看到它调用了属性的权限/范围,以及您收到的错误。如果您认为应该进行任何特定的编辑,或者可以添加一些内容以使其更有帮助/更易​​于阅读,我鼓励您使用页面右下角的按钮提交反馈。 :)
    • @robDevereux 我按照你的建议做了,但一路上收到各种警告你的连接不安全 docs-feedback.aws.amazon.com 的所有者配置了他们的网站不正确。为保护您的信息不被窃取,Firefox 未连接到本网站。了解更多... 报告此类错误以帮助 Mozilla 识别和阻止恶意网站
    • 上述文档仍然没有明确说明您可以在哪里设置属性权限。我发现它在常规设置 -> 应用程序客户端 -> 显示详细信息 -> 设置属性读写权限链接
    • 我不清楚这些设置应该如何设置。我检查了每个框,但它仍然不起作用。
    • 能否得到更详细的错误信息。比如,哪个属性导致了这个问题?
    【解决方案2】:

    只需突出显示answer from @mvandillen

    常规设置 -> 应用客户端 -> 显示详情 -> 设置属性读写权限链接

    【讨论】:

    • 这很有魅力!!谢谢
    【解决方案3】:

    对于任何偶然发现这个问题的人:

    像其他人建议的那样,您应该启用可写属性。但如果这不起作用,请确保使用 custom: 前缀:

    await Auth.signUp({
          username: email,
          password: password,
          attributes: {
            'custom:firstName': firstName,
            'custom:lastName': lastName,
            'custom:countryCode': countryCode
          }
        })
    

    【讨论】:

      【解决方案4】:

      在 ASP.NET Core 中使用 Amazon.Extensions.CognitoAuthentication,您必须添加:

      var user = _pool.GetUser(model.Email)
      user.Attributes.Add("name", model.Name);
      

      这里name是自定义属性

      【讨论】:

        【解决方案5】:

        我的情况有点不同。我正在使用 Amplify angular 组件,并且没有任何自定义属性(标准电子邮件登录类型)。

        事实证明,注册字段的键是区分大小写的。对于大写的“电子邮件”键,我会看到错误。 下面是注册配置

        emailSignUpConfig = {
                header: 'Sign up header',
                hideAllDefaults: true,
                defaultCountryCode: '1',
                signUpFields: [
                    {
                        label: 'Email',
                        key: 'email', //the email should be in lower case
                        required: true,
                        displayOrder: 1,
                        type: 'string',
                    },
                    {
                        label: 'Password',
                        key: 'password',
                        required: true,
                        displayOrder: 2,
                        type: 'password',
                    },
                ],
            };
        

        【讨论】:

          【解决方案6】:

          启用可写属性后,这对我有用

              const user = {
               username:this.username,
               password:this.password,     
                attributes:{
                email:this.email,
                'custom:field1': this.field1,
                'custom:field2': this.field2,
                'custom:field3': this.field3
               }
             }
          
          
                       OR
          
          
          
             const user = {
               username:this.username,
               password:this.password,
               email:this.email,
                attributes:{
                'custom:field1': this.field1,
                'custom:field2': this.field2,
                'custom:field3': this.field3
               }
             }
          

          【讨论】:

            【解决方案7】:

            我只想添加有关 Android 和 Amplify 的 Auth 库的列表。除了通过 AWS 控制台(常规设置 -> 应用程序客户端 -> 显示详细信息 -> 设置属性读写权限链接)启用上面指定的自定义角色外,您还需要指定 AuthUserAttributeKey.custom() 下的自定义角色具有字符串 custom:附加到您的自定义字段。我的假设是,鉴于函数调用名称,这将被排除在外。有点误导,但我希望这对其他人有帮助。

            TLDR;

            改变了这个:

            AuthUserAttributeKey.custom("role");
            

            到:

            AuthUserAttributeKey.custom("custom:role");
            

            【讨论】:

              【解决方案8】:

              就我而言,问题在于访问名称不正确的变量。总结一下我的步骤如下。

              1. 添加属性:常规设置->属性->添加自定义属性链接

              2. 确保您设置读/写权限:常规设置 -> 应用客户端 -> 显示详细信息 -> 设置属性权限链接

              3. 不要忘记使用正确的名称访问您的变量。例如,如果您的变量名为 'foo',您应该将其作为 'custom:foo' 获取,如下所示。

              【讨论】:

                猜你喜欢
                • 2017-07-06
                • 2019-04-27
                • 2018-12-18
                • 2019-10-15
                • 2020-06-04
                • 2020-01-11
                • 2013-08-03
                • 2015-02-01
                • 2018-07-07
                相关资源
                最近更新 更多