如何使用托管UI从C#代码更新Cognito用户属性?

0

【以下的问题经过翻译处理】 我正在使用Cognito Hosted UI与C#项目,可以获取当前用户的详细信息,但无法更新当前用户的属性。

我正在寻找解决方案。我相信可以使用Amazon.CognitoIdentityProvider中的UpdateUserAttributesAsync函数,但我不知道如何从UpdateUserAttributesRequest类中获取AccessToken。

您能否指导或告诉我如何修改用户属性?

AWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKey");

AmazonCognitoIdentityProviderClient cognitoClient = new(awsCredentials);

UpdateUserAttributesRequest updateUserAttributesRequest = new()
{ 
     //AccessToken = Azure.Core.Token
 };

List<AttributeType> userAttributes = new()
{
       new AttributeType() { Name = "email", Value = "allo@test.com" },
       new AttributeType() { Name = "custom:PhoneNumber", Value = "888 888 8888" }
};

updateUserAttributesRequest.UserAttributes = userAttributes;
var testss = await cognitoClient.UpdateUserAttributesAsync(updateUserAttributesRequest);
profile picture
专家
已提问 5 个月前8 查看次数
1 回答
0

【以下的回答经过翻译处理】 您可以通过下面的函数获取accessToken,这里是stackoverflow答案的链接。 https://stackoverflow.com/questions/66258459/how-to-get-aws-cognito-access-token-with-username-and-password-in-net-core-3-1

public static async Task<string> GetCredsAsync()
        {
            AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast1);
            CognitoUserPool userPool = new("", "", provider, "");
            CognitoUser user = new("", "", userPool, provider, "");
            InitiateSrpAuthRequest authRequest = new()
            {
                Password = ""
            };

            AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
            var accessToken = authResponse.AuthenticationResult.AccessToken;
            return accessToken;
        }

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则