Example #1
0
        //
        // Given user details, register a user to the cognito user pool.
        //
        public async Task <bool> registerUser(string nickname, string password, string email)
        {
            var provider = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), Amazon.RegionEndpoint.EUWest2);
            var userPool = new CognitoUserPool(Statics.poolID, Statics.clientID, provider);

            var useratts = new Dictionary <string, string>();

            useratts.Add("nickname", nickname);

            Console.WriteLine($"Adding user {email} nickname {nickname}");
            try
            {
                await userPool.SignUpAsync(email, password, useratts, new Dictionary <string, string>());

                Console.WriteLine("Done sign up async.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong.");
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
Example #2
0
        public void Run(APIGatewayProxyRequest request, APIGatewayProxyResponse response, FinanceUser user)
        {
            var model = JsonConvert.DeserializeObject <SignupModel>(request.Body);

            if (!model.AgreedToLicense)
            {
                throw new Exception("agreedToLicense must be true");
            }
            var provider = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1);
            var userPool = new CognitoUserPool(Configuration.FINANCE_API_COGNITO_USER_POOL_ID, Configuration.FINANCE_API_COGNITO_CLIENT_ID, provider);
            var result   = userPool.SignUpAsync(
                model.Email.ToLower(),
                model.Password,
                new Dictionary <string, string>(),
                new Dictionary <string, string>());

            result.Wait();
            var userService = new UserService();

            userService.CreateUser(model.Email, model.AgreedToLicense, IpLookup.GetIp(request));
            var json = new JObject
            {
                { "status", "Your user has successfully been created. " +
                  $"Your user name is {model.Email}. " +
                  "A confirmation link has been sent to your email from [email protected]. " +
                  "You need to click the verification link in the email before you can login." }
            };

            response.Body = json.ToString();
        }
        /// <summary>
        /// Registers the specified <paramref name="user"/> in Cognito with the given password,
        /// as an asynchronous operation. Also submits the validation data to the pre sign-up lambda trigger.
        /// </summary>
        /// <param name="user">The user to create.</param>
        /// <param name="password">The password for the user to register with</param>
        /// <param name="validationData">The validation data to be sent to the pre sign-up lambda triggers.</param>
        /// <returns>
        /// The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/>
        /// of the operation.
        /// </returns>
        public virtual async Task <IdentityResult> CreateAsync(TUser user, string password, IDictionary <string, string> validationData, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                await _pool.SignUpAsync(user.UserID, password, user.Attributes, validationData).ConfigureAwait(false);

                return(IdentityResult.Success);
            }
            catch (AmazonCognitoIdentityProviderException e)
            {
                return(IdentityResult.Failed(_errorDescribers.CognitoServiceError("Failed to create the Cognito User", e)));
            }
        }
Example #4
0
        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());
        }