How can I update a Cognito User Attribute from C# code using Hosted UI?

0

I'm using Cognito Hosted UI with a C# project i can get current user details, but i can't update the current user attributes. I'm on a track to find the solution. I believe using the UpdateUserAttributesAsync function from Amazon.CognitoIdentityProvider, but I'm not sure how to get the AccessToken from the UpdateUserAttributesRequest class. Would it be possible to guide me or tell me how to modify the attributes of a user?

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);
已提问 1 年前1148 查看次数
1 回答
0
已接受的回答

Hi, i found my answer. You can get the accessToken from this function below and here is the link for stackoverflow answer. 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;
        }
已回答 1 年前

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

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

回答问题的准则