Esempio n. 1
0
        public async Task <IActionResult> Register([FromForm] ApiRegisterModel model)
        {
            if (model == null || model.GetType().GetProperties().Any(p => string.IsNullOrEmpty(p.GetValue(model).ToString())))
            {
                return(BadRequest("模型验证失败"));
            }
            if (_dbContext.Users.Where(u => u.Email == model.Email).Any())
            {
                return(BadRequest("邮箱已注册"));
            }
            if (_dbContext.Users.Where(u => u.UserName == model.UserName).Any())
            {
                return(BadRequest("用户名已注册"));
            }
            var user = new EaUser {
                UserName = model.UserName, Email = model.Email, CreateTime = DateTime.Now, Type = "default"
            };
            var result = await _userManager.CreateAsync(user, model.Pwd);

            if (result.Succeeded)
            {
                //_logger.LogInformation("User created a new account with password.");
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                var res         = await _emailSender.SendEmailConfirmationAsync(model.Email, "");

                return(Ok());
            }
            return(BadRequest(result.Errors));
        }
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var email = model.Email.ToLower();
                if (_dbContext.Users.Any(u => u.Email.ToLower() == email))
                {
                    ModelState.AddModelError(string.Empty, "该邮箱已注册!");
                    return(View(model));
                }
                var user = new EaUser {
                    UserName = model.Name, Email = model.Email, CreateTime = DateTime.Now, Type = "default"
                };
                var result = await _userManager.CreateAsync(user, model.Password);

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

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    var res         = await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    //await _signInManager.SignInAsync(user, isPersistent: true);
                    //_logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

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

#if (!DEBUG)
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(Input.Email, callbackUrl);
#endif

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

                    return(LocalRedirect(Url.GetLocalUrl(returnUrl)));
                }
                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> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                User user = new User {
                    UserName = model.Email, Email = model.Email
                };
                IdentityResult result = await _userManager.CreateAsync(user, model.Password);

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

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 5
0
        private async Task SendEmailConfirmationToken(UserEf user)
        {
            string code = await this.userService.GenerateEmailConfirmationTokenAsync(user);

            string callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

            await emailSender.SendEmailConfirmationAsync(user.Email, callbackUrl);
        }
Esempio n. 6
0
        public async Task <IActionResult> Register(RegisterViewModel model,
                                                   string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            User user = await userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                this.TempData.AddErrorMessage($"E-mail address '{model.Email}' is already taken.");
                return(View(model));
            }

            user = new User
            {
                UserName  = model.Username,
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Email     = model.Email,
                Birthdate = model.Birthdate,
                Genre     = model.Genre
            };

            IdentityResult result = await userManager.CreateAsync(user, model.Password);

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

                string code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                string callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

                await emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                //await signInManager.SignInAsync(user, isPersistent: false);
                logger.LogInformation("User created a new account with password.");

                if (returnUrl is null)
                {
                    TempData.AddSuccessMessage($"The registration is successfull. Please, verify your e-mail address {model.Email} before proceeding.");

                    return(View(model));
                }

                return(this.RedirectToLocal(returnUrl));
            }

            // If we got this far, something failed, redisplay form
            this.AddErrors(result);
            return(View(model));
        }
Esempio n. 7
0
        public async Task <IdentityResult> CreateUserAsync(
            RegisterViewModel registerViewModel,
            IUrlHelper urlHelper,
            string callAction,
            string controller,
            string scheme)
        {
            ApplicationUser newuser = _mapper.Map <RegisterViewModel, ApplicationUser>(registerViewModel);
            IdentityResult  result  = await _userManager.CreateAsync(newuser, password : registerViewModel.Password);

            if (result.Succeeded)
            {
                ApplicationUser user = await _userManager.FindByEmailAsync(newuser.Email);

                string code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                string callBackUrl = GenerateCallbackLink(urlHelper, callAction, controller, user.Id, code, scheme);
                await _emailSenderService.SendEmailConfirmationAsync(new EmailConfirmationModel { Email = user.Email, Link = callBackUrl });

                return(result);
            }
            return(result);
        }
Esempio n. 8
0
        public async Task <IActionResult> SendVerificationEmail(ProfileViewModel model)
        {
            var user = await this.userService.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{this.userService.GetUserId(User)}'.");
            }

            var code = await this.userService.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
            var email       = user.Email;
            await emailSender.SendEmailConfirmationAsync(email, callbackUrl);

            StatusMessage = "Verification email sent. Please check your email.";
            return(RedirectToAction(nameof(Profile)));
        }
Esempio n. 9
0
        public async Task <IActionResult> OnPostSendVerificationEmailAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
            await _emailSender.SendEmailConfirmationAsync(user.Email, callbackUrl);

            StatusMessage = "Verification email sent. Please check your email.";
            return(RedirectToPage());
        }
Esempio n. 10
0
        public async Task <IActionResult> SendVerificationEmail(IndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
            var email       = user.Email;
            await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

            StatusMessage = "确认邮件已发出.";
            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 11
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = AddErrors() }));
            }

            var result = await _userEngine.CreateUser(model);

            if (!result.Succeeded)
            {
                return(BadRequest(new { message = result.StatusMessage }));
            }

            var clientPath = _config["clientSite"];

            if (!string.IsNullOrEmpty(clientPath))
            {
                var encodedCode = UrlEncoder.Default.Encode(HttpUtility.HtmlEncode(result.EmailCode));
                var callbackUrl = $"{Request.Scheme}://{clientPath}/confirmemail?id={result.UserId}&code={encodedCode}";
                await _emailSenderService.SendEmailConfirmationAsync(result.Email, callbackUrl);
            }
            return(Ok());
        }