Exemple #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new CognitoUser()
                {
                    UserName = model.Email, Email = model.Email, Password = model.Password
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    // Uncomment this if you just want to let the user login. Commented out, means they'll need to login and confirm the code in their email.
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // TODO: Redirect them to a page saying sign-up succeeded and they need to get a code from their email.

                    return(RedirectToAction("Registered"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async void Test_GivenAUser_WhenCreate_ThenResponseIsNotAltered()
        {
            var cognitoUser = GetCognitoUser();

            userStoreMock.Setup(mock => mock.CreateAsync(It.IsAny <CognitoUser>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
            userStoreMock.Setup(mock => mock.CreateAsync(It.IsAny <CognitoUser>(), It.IsAny <string>(), It.IsAny <IDictionary <string, string> >(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
            passwordValidatorsMock.Setup(mock => mock.ValidateAsync(It.IsAny <CognitoUserManager <CognitoUser> >(), It.IsAny <CognitoUser>(), It.IsAny <string>())).Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
            var output = await userManager.CreateAsync(cognitoUser).ConfigureAwait(false);

            Assert.Equal(IdentityResult.Success, output);
            output = await userManager.CreateAsync(cognitoUser, "password").ConfigureAwait(false);

            Assert.Equal(IdentityResult.Success, output);
            userStoreMock.Verify();
        }
Exemple #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = _pool.GetUser(Input.UserName);
                user.Attributes.Add(CognitoAttributesConstants.Email, Input.Email);

                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToPage("./ConfirmAccount"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Exemple #4
0
        public async Task <IActionResult> SignUp(SignupModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            CognitoUser user = _pool.GetUser(model.Email);

            if (user.Status != null)
            {
                ModelState.AddModelError("UserExist", "User already exist!");
                return(View(model));
            }

            user.Attributes.Add(CognitoAttribute.Name.AttributeName, model.Email);

            IdentityResult createdUser = await _userManager.CreateAsync(user, model.Password);

            if (createdUser.Succeeded)
            {
                return(RedirectToAction("Confirm"));
            }

            return(View());
        }
Exemple #5
0
        public async Task <IActionResult> SignUp(SignUpViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var user = _cognitoUserPool.GetUser(viewModel.Email);

            if (user.Status != null)
            {
                ModelState.AddModelError("UserExists", "User with this email already exists");
                return(View(viewModel));
            }

            user.Attributes.Add(CognitoAttribute.Name.AttributeName, viewModel.Email);

            var result = await _cognitoUserManager.CreateAsync(user, viewModel.Password);

            if (result.Succeeded)
            {
                return(RedirectToAction("Confirm"));
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }


            return(View(viewModel));
        }
Exemple #6
0
        public async Task <IActionResult> Create([FromForm] RegisterViewModel model)
        {
            IActionResult result = View();

            if (ModelState.IsValid)
            {
                var user = _userPool.GetUser(model.Email);
                user.Attributes.Add(CognitoAttribute.Email.AttributeName, model.Email);
                user.Attributes.Add(CognitoAttribute.GivenName.AttributeName, model.FirstName);
                user.Attributes.Add(CognitoAttribute.FamilyName.AttributeName, model.LastName);
                user.Attributes.Add(CognitoAttribute.Name.AttributeName, $"{model.FirstName} {model.LastName}");
                user.Attributes.Add(CognitoAttribute.PhoneNumber.AttributeName, $"+1{model.PhoneNumber}");
                var createResult = await _userManager.CreateAsync(user, model.Password);

                if (createResult.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    result = RedirectToAction(nameof(Confirm));
                }
                else
                {
                    foreach (var error in createResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            return(result);
        }
        public async Task <IActionResult> SignUp(Signup model)
        {
            if (ModelState.IsValid)
            {
                var user = pool.GetUser(model.Email);
                if (user.Status != null) // user exists
                {
                    ModelState.AddModelError("UserExists", "User with this email already exists");
                    return(View(model));
                }

                try
                {
                    user.Attributes.Add("name", model.Email);
                    var createdUser = await userManager.CreateAsync(user, model.Password).ConfigureAwait(false);

                    if (createdUser.Succeeded)
                    {
                        return(RedirectToAction("Confirm"));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Signup", "Unable to create new user");
                    ModelState.AddModelError("SignupUnhandleError", ex.Message);
                }
            }
            return(View("Signup", model));
        }
Exemple #8
0
        public async Task <IActionResult> Signup(SignUpModel model)
        {
            if (ModelState.IsValid)
            {
                var user = pool.GetUser(model.Email);
                if (user.Status != null)
                {
                    ModelState.AddModelError("UserExists", "User with this email already exists");
                    return(View(model));
                }

                user.Attributes.Add(CognitoAttribute.Name.AttributeName, model.Email);
                userManager.PasswordValidators.Clear();
                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Confirm"));
                }
                else
                {
                    foreach (var identityError in result.Errors)
                    {
                        ModelState.AddModelError(identityError.Code, identityError.Description);
                    }
                }
            }

            return(View());
        }
Exemple #9
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                //set origin attributew value for the user
                var user = _pool.GetUser(Input.UserName);
                user.Attributes.Add(CognitoAttribute.Email.AttributeName, Input.Email);
                user.Attributes.Add(CognitoAttribute.Address.AttributeName, "default");
                user.Attributes.Add("custom:AddressLine1", "default");
                user.Attributes.Add("custom:AddressLine2", "default");
                user.Attributes.Add("custom:City", "default");
                user.Attributes.Add("custom:State", "default");
                user.Attributes.Add("custom:Country", "default");
                user.Attributes.Add("custom:ZipCode", "default");
                user.Attributes.Add(CognitoAttribute.BirthDate.AttributeName, "0000-00-00");
                user.Attributes.Add(CognitoAttribute.Gender.AttributeName, "default");
                user.Attributes.Add(CognitoAttribute.NickName.AttributeName, "default");
                user.Attributes.Add(CognitoAttribute.PhoneNumber.AttributeName, "+01234567890");
                user.Attributes.Add(CognitoAttribute.FamilyName.AttributeName, Input.LastName);
                user.Attributes.Add(CognitoAttribute.GivenName.AttributeName, Input.FirstName);
                //check if the email has been used
                var existuser = await _userManager.FindByEmailAsync(Input.Email);

                if (existuser != null)
                {
                    ModelState.AddModelError(string.Empty, "E-mail address has been used");
                    return(Page());
                }
                //creat account
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    await _signInManager.SignInAsync(user, isPersistent : false);


                    return(RedirectToPage("./ConfirmAccount"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public async Task <IActionResult> SignUP(SignUpModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _pool.GetUser(model.Email);
                if (user.Status != null)
                {
                    ModelState.AddModelError("UserExists", "User with this email already exists");
                }
                user.Attributes.Add(CognitoAttributesConstants.Name, model.Email);
                var createdUser = await _userManager.CreateAsync(user, model.Passsword).ConfigureAwait(false);

                if (createdUser.Succeeded)
                {
                    RedirectToAction("Confirm");
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> register(RegisterViewModel model, string returnUrl = null)
        {
            if (this.ModelState.IsValid)
            {
                var user = _pool.GetUser(model.UserName);
                user.Attributes.Add(CognitoAttribute.Email.AttributeName, model.Email);
                user.Attributes.Add(CognitoAttribute.Name.AttributeName, model.Name);

                var result = _userManager.CreateAsync(user, model.Password).Result;
                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("index", "Secure"));
                }
                //else
                //return BadRequest($"<ul>{string.Concat(result.Errors.Select(ms => $"<li>{ms.Description}</li>"))}</ul>");
            }

            //return BadRequest($"<ul>{string.Concat(this.ModelState.Values.Where(ms => ms.Errors.Any()).Select(ms => $"<li>{ms.Errors[0].ErrorMessage}</li>"))}</ul>");
            return(BadRequest());
        }
        public async Task <IActionResult> Signup(SignupModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _pool.GetUser(model.Email);

                if (user.Status != null)
                {
                    ModelState.AddModelError("UserExist", "User with this email already exist");

                    return(View(model));
                }

                user.Attributes.Add(CognitoAttribute.Name.AttributeName, model.Email);

                var createResult = await _userManager.CreateAsync(user, model.Password);

                if (createResult.Succeeded)
                {
                    return(RedirectToAction("Confirm"));
                }
            }
            return(View(model));
        }
        public async Task <IActionResult> Signup(SignupModel model)
        {
            if (ModelState.IsValid)
            {
                // Retrieves a new user with the pool configuration set up
                CognitoUser user = _userPool.GetUser(model.Email);
                if (user.Status != null)
                {
                    var msg = "User with this email already exists";
                    ModelState.AddModelError("UserExists", msg);
                    return(View(model));
                }

                // add attribute name to user as thats we I specified in Cognito
                user.Attributes.Add(CognitoAttribute.Name.AttributeName, model.Email);

                // create user
                var createdtUser = await _userManager.CreateAsync(user, model.Password);

                if (createdtUser.Succeeded)
                {
                    return(RedirectToAction("Confirm"));
                }
                else
                {
                    foreach (var error in createdtUser.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(await Task.FromResult(View()));
        }
Exemple #14
0
        public async Task <IActionResult> Signup(SignupModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _pool.GetUser(model.Email);

                //Check if user already exists
                if (user.Status != null)
                {
                    ModelState.AddModelError("UserExists", "User with this email already exsits");
                    return(View(model));
                }

                //Add in other required attribute (using Email Address as Name here)
                user.Attributes.Add(CognitoAttribute.Name.ToString(), model.Email);

                //Create User: If password is not included then a temp password will be allocated by Cognito
                //and the user informed by email so that they can change it.
                var createdUser = await _userManager.CreateAsync(user, model.Password);

                if (createdUser.Succeeded)
                {
                    return(RedirectToAction("Confirm", "Accounts", new ConfirmModel {
                        Email = model.Email
                    }));
                }

                foreach (var error in createdUser.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }
            }


            return(View(model));
        }