示例#1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            string isEnabledRegister = _appSettings.Value.EnableAdminRegister;

            if (bool.Parse(isEnabledRegister))
            {
                if (ModelState.IsValid)
                {
                    var user = new IdentityUser {
                        UserName = Input.Name, Email = Input.Email
                    };
                    var result = await _userManager.CreateAsync(user, Input.Password);

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

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

                        var bodyData = new Dictionary <string, string>
                        {
                            { "Title", "Registro de nuevo administrador." },
                            { "UserName", Input.Name },
                            { "Email", Input.Email }
                        };

                        _sendEmail.SendEmailRegister(bodyData,
                                                     "RegisterTemplate",
                                                     "Nuevo registro de administrador en " + _appSettings.Value.Client,
                                                     new List <string>()
                        {
                            _appSettings.Value.EmailAdminRegistration
                        }
                                                     );

                        //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                        //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

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

                        return(LocalRedirect(returnUrl));
                    }
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            else
            {
                _logger.LogInformation("Register is disabled.");
                _logger.LogInformation("User: "******".");
                ModelState.AddModelError(string.Empty, "Register is disabled.");
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }