public override async Task <Unit> Handle(CognitoFormatPasswordCommand request, CancellationToken cancellationToken) { var user = await _mediator.Send(new CognitoGetUserByEmailQuery { Email = request.Email }, cancellationToken); if (user == null) { throw new Exception("User not found by email: " + request.Email + ". Unable to reset password."); } using (var provider = new AmazonCognitoIdentityProviderClient(AwsId, AwsKey, RegionEndpoint.USEast1)) { if (!user.IsEmailVerified) { await provider.AdminUpdateUserAttributesAsync(new AdminUpdateUserAttributesRequest { UserAttributes = new List <AttributeType> { new AttributeType { Name = "email_verified", Value = "true" } }, Username = user.LegacyId.ToString(), UserPoolId = UserPoolId }, cancellationToken); } await provider.ForgotPasswordAsync(new ForgotPasswordRequest { ClientId = UserGroupClientId, Username = user.LegacyId.ToString() }, cancellationToken); } return(Unit.Value); }
/// <summary> /// Update User /// </summary> /// <param name="userInfo"></param> /// <returns></returns> public async Task UpdateUserAsync(UserInfo userInfo) { AdminUpdateUserAttributesRequest updateUserAttributesRequest = new AdminUpdateUserAttributesRequest() { Username = userInfo.UserName, UserPoolId = _appConfigInfo.AWSPoolId, UserAttributes = new List <AttributeType>() { new AttributeType() { Name = "custom:Role", Value = userInfo.Role }, new AttributeType() { Name = "custom:Groups", Value = string.Join(",", userInfo.Groups) } } }; try { var userCreateResult = await _provider.AdminUpdateUserAttributesAsync(updateUserAttributesRequest); } catch (UserNotFoundException) { throw new CcsSsoException("USERNAME_NOT_EXISTS"); } }
public override async Task <string> Handle(CognitoSignUpCommand request, CancellationToken cancellationToken) { using (var provider = new AmazonCognitoIdentityProviderClient(AwsId, AwsKey, RegionEndpoint.USEast1)) { var response = await provider.SignUpAsync(new SignUpRequest { Password = request.Password, ClientId = UserGroupClientId, Username = request.Email, UserAttributes = new List <AttributeType> { new AttributeType { Name = "email", Value = request.Email }, new AttributeType { Name = "preferred_username", Value = request.Username } } }, cancellationToken); if (response.HttpStatusCode != HttpStatusCode.OK) { throw new CognitoException("Failed to register user with email " + request.Email + "."); } await provider.AdminConfirmSignUpAsync(new AdminConfirmSignUpRequest { Username = response.UserSub, UserPoolId = UserPoolId }, cancellationToken); await provider.AdminUpdateUserAttributesAsync(new AdminUpdateUserAttributesRequest { UserAttributes = new List <AttributeType> { new AttributeType { Name = "email_verified", Value = "true" } }, Username = response.UserSub, UserPoolId = UserPoolId }, cancellationToken); return(response.UserSub); } }
public bool UpdateAttributes(ApiUpdateRequest updateInfo) { using AmazonCognitoIdentityProviderClient userProvider = GetCognitoIdentityProvider(); var request = new AdminUpdateUserAttributesRequest(); request.Username = updateInfo.UserName; request.UserPoolId = CognitoSettings.Values.UserPoolId; request.UserAttributes = updateInfo.Attributes.Select(data => new AttributeType() { Name = data.Key, Value = data.Value }).ToList(); userProvider.AdminUpdateUserAttributesAsync(request, CancellationToken.None).GetAwaiter().GetResult(); return(true); }
public async Task UpdateUser(string id, Dictionary <string, string> attributes, bool setEable = false) { if (attributes.Count > 0) { var list = attributes.Select(x => new AttributeType { Name = x.Key, Value = x.Value }).ToList(); var request = new AdminUpdateUserAttributesRequest { UserPoolId = Configurations.Cognito.CognitoPoolId, Username = id, UserAttributes = list, }; await provider.AdminUpdateUserAttributesAsync(request); } if (setEable) { await SetAccountEable(id, true); } }
public async Task <HttpStatusCode> AdminUpdateUserAttributesAsync(Profile profile) { var request = new AdminUpdateUserAttributesRequest { UserPoolId = _cognito.UserPool.Id, Username = $"{profile.Email}", UserAttributes = profile.GetCognitoAttributes().ToList() }; AdminUpdateUserAttributesResponse response; try { response = await _provider.AdminUpdateUserAttributesAsync(request); } catch (Exception e) { Console.WriteLine($"Exception: {e}"); throw new Exception(); } return(response.HttpStatusCode); }
public async void SignUpNewUser(string email, string password, string familyName, string firstName, string phoneNumber, string deviceId) { AnonymousAWSCredentials credentials = new AnonymousAWSCredentials(); AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(credentials, Amazon.RegionEndpoint.USEast2); CognitoUserPool pool = new CognitoUserPool(ConfigurationManager.AppSettings["USERPOOL_ID"], ConfigurationManager.AppSettings["CLIENT_ID"], provider, ""); // Based on latest user pool API // https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPool.html // https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_VerificationMessageTemplateType.html // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html Dictionary <string, string> userAttributes = new Dictionary <string, string>(StringComparer.Ordinal) { { "email", email }, { "family_name", familyName }, { "given_name", firstName }, }; Dictionary <string, string> validationData = new Dictionary <string, string>(StringComparer.Ordinal) { { "email", email } }; await pool.SignUpAsync(email, password, userAttributes, validationData).ConfigureAwait(false); //Get the UsersVerificationCode programatically. Task <AdminConfirmSignUpResponse> myresponse = provider.AdminConfirmSignUpAsync(new AdminConfirmSignUpRequest { UserPoolId = ConfigurationManager.AppSettings["USERPOOL_ID"], Username = email }); AdminUpdateUserAttributesRequest i = new AdminUpdateUserAttributesRequest(); i.UserAttributes.Add(new AttributeType { Name = "email_verified", Value = "true" }); i.UserPoolId = ConfigurationManager.AppSettings["USERPOOL_ID"]; i.Username = email; AdminUpdateUserAttributesResponse T = await provider.AdminUpdateUserAttributesAsync(i); Debug.Print(T.ToString()); // client.adminUpdateUserAttributes({ // UserAttributes: // [{ // Name: 'phone_number_verified', // Value: 'true' // }, { // Name: 'email_verified', // Value: 'true' // } // // other user attributes like phone_number or email themselves, etc //], //UserPoolId: 'COGNITO_USER_POOL_ID_HERE', //Username: '******' //myresponse.res. // Debug.Print(myresponse.Result.ToString()); }