//创建用户
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            IdentityUser user = new IdentityUser()
            {
                Email    = userModel.Email,
                UserName = userModel.Email,
            };

            //该方法有两个重载
            //Task<IdentityResult>CreateAsync(TUser user)
            //Task<IdentityResult>CreateAsync(Tuser user, string password)
            IdentityResult result = await _userManager.CreateAsync(user, userModel.Password);

            //如果账户创建成功,生成token,让用户去验证邮件
            if (result.Succeeded)
            {
                string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                if (!string.IsNullOrEmpty(token))
                {
                    await SendEmailConfirmationEmail(user, token);
                }
            }
            return(result);
        }
        public async Task <ActionResult> Create(SignUpUserModel userModel, IFormCollection collection)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = new ApplicationUser
                {
                    FirstName   = userModel.LastName,
                    LastName    = userModel.LastName,
                    Email       = userModel.Email,
                    UserName    = userModel.Email,
                    DateOfBirth = userModel.DateOfBirth,
                    CreatedOn   = System.DateTime.UtcNow,
                    UpdatedOn   = System.DateTime.UtcNow,
                };
                var result = await userManager.CreateAsync(user, userModel.Password);

                if (result.Succeeded)
                {
                    ViewBag.IsSuccess = true;
                    ViewBag.message   = "User created";
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return(View(userModel));
        }
Exemple #3
0
        public async Task <ActionResult <UserDTO> > PostSignUp(SignUpUserModel signUpUserModel)
        {
            UserDTO userDTO = this._mapper.Map <UserDTO>(signUpUserModel);
            await _iUserUpdateService.Value.CustomCreateAsync(userDTO, signUpUserModel.Password);

            return(Ok(new Response <UserDTO>(userDTO)));
        }
Exemple #4
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName,
                Email     = userModel.Email,
                UserName  = userModel.Email
            };

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

            if (result.Succeeded)
            {
                await GenerateEmailConfirmationTonkenAsync(user);

                //Email confirm token
                //var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                //if (!string.IsNullOrEmpty(token))
                //{
                //    await SendEmailConfirmationEmail(user, token);

                //}
            }

            return(result);
        }
        public async Task <IActionResult> SignUp(SignUpUserModel userModel)
        {
            if (ModelState.IsValid)
            {
                var result = await _accountManager.CreateUserAsync(userModel);

                if (!result.Succeeded)
                {
                    foreach (var errorMessage in result.Errors)
                    {
                        if (errorMessage.Code != "DuplicateUserName")
                        {
                            ModelState.AddModelError("", errorMessage.Description);
                        }
                    }

                    return(View(userModel));
                }

                ModelState.Clear();

                return(RedirectToAction("ConfirmEmail", "Account", new { email = userModel.Email }));
            }
            return(View());
        }
        public async Task <IActionResult> SignUp(SignUpUserModel signUpUserModel)
        {
            if (ModelState.IsValid)
            {
                //Tworzenie nowego użytkownika
                var user = new ApplicationUser()
                {
                    FirstName = signUpUserModel.FirstName,
                    LastName  = signUpUserModel.LastName,
                    Address   = signUpUserModel.Address,
                    City      = signUpUserModel.City,
                    Email     = signUpUserModel.Email,
                    UserName  = signUpUserModel.Email
                };
                var identityResult = await _userManager.CreateAsync(user, signUpUserModel.Password);



                if (identityResult.Succeeded)
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View(signUpUserModel));
        }
Exemple #7
0
        public async Task <IActionResult> PutSignUpUserModel(int id, SignUpUserModel signUpUserModel)
        {
            if (id != signUpUserModel.Id)
            {
                return(BadRequest());
            }

            _context.Entry(signUpUserModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SignUpUserModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #8
0
        public async Task SignUpAdmin(SignUpUserModel signUpUserModel)
        {
            var user = new User
            {
                Email    = signUpUserModel.Email,
                UserName = signUpUserModel.Username
            };

            if (signUpUserModel.Password == signUpUserModel.VerifyPassword)
            {
                var result = await userManager.CreateAsync(user, signUpUserModel.Password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "ADMIN");
                }
                else
                {
                    throw new ArgumentException("Account wasn't made!");
                }
            }
            else
            {
                throw new ArgumentException("PasswordVerification is different from Password!");
            }
        }
Exemple #9
0
        public async Task <IActionResult> SignUp([FromBody] SignUpUserModel signUpUserModel)
        {
            var signUpUser = signUpUserModel.ConvertToSignUp();

            if (!signUpUser.IsValid())
            {
                return(CustomResponse(signUpUser.ValidationResult));
            }

            var user = new IdentityUser
            {
                UserName       = signUpUser.Email,
                Email          = signUpUser.Email,
                EmailConfirmed = true
            };
            var result = await _userManager.CreateAsync(user, signUpUser.Password);

            if (result.Succeeded)
            {
                return(CustomResponse(await GenerateJWT(user.Email)));
            }
            foreach (var erro in result.Errors)
            {
                AdicionarErroProcessamento(erro.Description);
            }
            return(CustomResponse());
        }
Exemple #10
0
        public CustomJsonResult SignUp(SignUpUserModel model)
        {
            long userId = this.userService.CreateUser(model.ToCreateUserDto());

            this.Session.SetCurrentUserId(userId);
            FormsAuthentication.SetAuthCookie(model.Name, true);

            return(this.SuccessResult());
        }
 public ActionResult<object> Post(
     [FromBody] SignUpUserModel user,
     [FromServices] AccessManager accessManager)
 {
     var result = accessManager.ValidateSignUp(user);
     return new
     {
         Result = result
     };
 }
Exemple #12
0
 public static CreateUserDto ToCreateUserDto(this SignUpUserModel signUpModel)
 {
     return(new CreateUserDto
     {
         LoginName = signUpModel.Name,
         FirstName = signUpModel.FirstName,
         LastName = signUpModel.LastName,
         Password = signUpModel.Password
     });
 }
Exemple #13
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new IdentityUser()
            {
                Email    = userModel.Email,
                UserName = userModel.Email,
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
        public async Task GenerateEmailConfirmationTokenAsync(SignUpUserModel signUpModel, ApplicationUser userModel, DeviceParser device)
        {
            // Generate a token using Identity Core.
            string token = await _userManager.GenerateEmailConfirmationTokenAsync(userModel);

            // If the token is valid.
            if (!string.IsNullOrWhiteSpace(token))
            {
                await SendEmailConfirmationTokenAsync(signUpModel, userModel, device, token);
            }
        }
Exemple #15
0
        public async Task <IHttpActionResult> PostSignUpUserModel(SignUpUserModel signUpUserModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var userId    = Guid.NewGuid().ToString();
                var hashedPsw = _pswHashing.ComputeHash(signUpUserModel.Password);

                db.SignInModels.Add(new SignInModel
                {
                    UserId   = userId,
                    UserName = signUpUserModel.UserName,
                    Password = hashedPsw
                });

                db.SignUpUserModels.Add(new SignUpUserModel
                {
                    Name     = signUpUserModel.Name,
                    Surname  = signUpUserModel.Surname,
                    UserId   = userId,
                    UserName = signUpUserModel.UserName,
                    Email    = signUpUserModel.Email,
                    Password = hashedPsw
                });
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                throw;
            }

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (SignUpUserModelExists(signUpUserModel.UserName))
                {
                    return(StatusCode(HttpStatusCode.Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = signUpUserModel.UserId }, signUpUserModel));
        }
Exemple #16
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel UserModel)
        {
            var user = new AppilicationUser()
            {
                firstName  = UserModel.firstName,
                LaststName = UserModel.LastName,
                Email      = UserModel.LoginId,
                UserName   = UserModel.LoginId,
            };
            var result = await _userManager.CreateAsync(user, UserModel.Password);

            return(result);
        }
Exemple #17
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                Email     = userModel.EmailId,
                UserName  = userModel.EmailId,
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
Exemple #18
0
        public async Task <IActionResult> SignUpAdmin([FromBody] SignUpUserModel model)
        {
            try
            {
                await authenticationManager.SignUpAdmin(model);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest("Something failed"));
            }
        }
Exemple #19
0
 public async Task <ActionResult <SignUpUserModel> > PostSignUpUserModel([FromForm] SignUpUserModel signUpUserModel)
 {
     try
     {
         _context.signUpUsers.Add(signUpUserModel);
         await _context.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     return(StatusCode(201));
     //return CreatedAtAction("GetSignUpUserModel", new { id = signUpUserModel.Id }, signUpUserModel);
 }
Exemple #20
0
        public async Task <IHttpActionResult> DeleteSignUpUserModel(string id)
        {
            SignUpUserModel signUpUserModel = await db.SignUpUserModels.FindAsync(id);

            if (signUpUserModel == null)
            {
                return(NotFound());
            }

            db.SignUpUserModels.Remove(signUpUserModel);
            await db.SaveChangesAsync();

            return(Ok(signUpUserModel));
        }
Exemple #21
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                FirstName      = userModel.FirstName,
                LastName       = userModel.LastName,
                Email          = userModel.Email,
                UserName       = userModel.Email,
                EmailConfirmed = true,
                UserImagePath  = "KnightOfGood.jpg"
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                FirstName   = userModel.FirstName,
                LastName    = userModel.LastName,
                ContactNo   = userModel.ContactNo,
                Email       = userModel.Email,
                UserName    = userModel.UserName,
                PhoneNumber = userModel.ContactNo
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
Exemple #23
0
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName,
                Email     = userModel.Email,
                UserName  = userModel.UserName,
                Age       = userModel.Age,
                City      = userModel.City,
                Country   = userModel.Country,
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel)
        {
            var user = new ApplicationUser()
            {
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName,
                Email     = userModel.Email,
                UserName  = userModel.Email
            };
            var result = await _userManager.CreateAsync(user, userModel.Password);

            if (result.Succeeded)
            {
                await GenerateEmailConfirmationTokenAsync(user);
            }
            return(result);
        }
        public async Task <IdentityResult> CreateUserAsync(SignUpUserModel userModel, DeviceParser device)
        {
            try
            {
                #region Create a user with some default values.

                // Create a new application user.
                ApplicationUser user = new()
                {
                    BackgroundColor      = "#FFFFFF",
                    DarkTheme            = "false",
                    UserData             = string.Empty,
                    PendingNotifications = string.Empty,
                    UserName             = userModel.Username,
                    Email               = userModel.Email,
                    LogoutThreshold     = 0,
                    AccountCreationDate = DateTime.UtcNow.ToString(),
                    LastPasswordUpdate  = DateTime.UtcNow.ToString()
                };

                #endregion Create a user with some default values.

                // Use Identity Core to create the user.
                IdentityResult result = await(_userManager.CreateAsync(user, userModel.Password));
                if (result.Succeeded)
                {
                    // add them to default role.
                    await _userManager.AddToRoleAsync(user, GoliathRoles.Default);

                    // Send them a token.
                    await GenerateEmailConfirmationTokenAsync(userModel, user, device);
                }
                _logger.LogInformation($"Created user \"{user.Id}\" with username \"{user.UserName}\" and email \"{user.Email}\".");
                return(result);
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to create new user.");
                return(IdentityResult.Failed(
                           new IdentityError()
                {
                    Code = e.TargetSite.Name,
                    Description = "Could not create account."
                }));
            }
        }
Exemple #26
0
        public async Task <IActionResult> SignUp(SignUpUserModel userModel)
        {
            if (ModelState.IsValid)
            {
                var result = await _accountRepository.CreateUserAsync(userModel);

                if (!result.Succeeded)
                {
                    foreach (var errorMessage in result.Errors)
                    {
                        ModelState.AddModelError("", errorMessage.Description);
                    }
                }
                ModelState.Clear();
            }
            return(View());
        }
Exemple #27
0
        public async Task <IActionResult> Register(SignUpUserModel model)
        {
            ViewData["ButtonID"] = ButtonID.Register;

            // Check if fields match checks.
            if (!ModelState.IsValid)
            {
                return(View());
            }

            // Model State Valid; Check Captcha
            if (!await _captcha.IsCaptchaValidAsync())
            {
                ModelState.AddModelError(_captcha.CaptchaValidationError().Key, _captcha.CaptchaValidationError().Value);
                return(View(model));
            }

            // Attempt to create a new user.
            IdentityResult result = await _accountRepository.CreateUserAsync(model,
                                                                             new DeviceParser(GetClientUserAgent(), GetRemoteClientIPv4()));

            if (result.Succeeded)
            {
                // Registration is Valid.
                ModelState.Clear();
                await _captcha.CacheNewCaptchaValidateAsync();

                // Send the user to the index with register tempdata.
                TempData[TempDataKeys.Redirect] = RedirectPurpose.RegisterSuccess;
                _logger.LogInformation($"USER {model.Username} was created.");
                // Send back to index.
                return(RedirectToAction(nameof(Index)));
            }
            // Registration Failed

            _captcha.DeleteCaptchaCookie();
            // For every error that is created during registration we add that to the eventual
            // bootstrap modal.
            foreach (IdentityError errorMessage in result.Errors)
            {
                ModelState.AddModelError(string.Empty, errorMessage.Description);
            }

            return(View(model));
        }
        public async Task <IActionResult> SignUp(SignUpUserModel signUpUserModel)
        {
            if (ModelState.IsValid)
            {
                var result = await _accountRepository.CreateUserAsync(signUpUserModel);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                    ;
                }
                ModelState.Clear();
                //RedirectToAction("confirm-email", new { email = signUpUserModel.Email });
            }
            return(View(signUpUserModel));
        }
Exemple #29
0
        public async Task <IActionResult> Signup(SignUpUserModel signUpUserModel)
        {
            if (ModelState.IsValid)
            {
                var result = await _accoutnRepository.CreateUserAsync(signUpUserModel);

                if (!result.Succeeded)
                {
                    foreach (var errorMessage in result.Errors)
                    {
                        ModelState.AddModelError("", errorMessage.Description);
                    }
                    return(View(signUpUserModel));
                }
                ModelState.Clear();
            }
            //return View(signUpUserModel);
            return(RedirectToAction(nameof(Signin)));
        }
Exemple #30
0
        public async Task <IActionResult> SignUp(SignUpUserModel userModel)
        {
            if (ModelState.IsValid)//işlem başarılı ise
            {
                var result = await _accountRepository.CreateUserAsync(userModel);

                if (!result.Succeeded)
                {
                    foreach (var errorMessage in result.Errors)
                    {
                        ModelState.AddModelError("", errorMessage.Description);
                    }
                    return(View(userModel));
                }
                ModelState.Clear();
                return(RedirectToAction("ConfirmEmail", new { email = userModel.Email }));
            }
            return(View());
        }