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);
asked a year ago1120 views
1 Answer
0
Accepted Answer

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;
        }
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions