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);
posta un anno fa1148 visualizzazioni
1 Risposta
0
Risposta accettata

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;
        }
con risposta un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande