Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null, bool isAdmin = false)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };

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

                var member = new MemberHelper();

                bool memberResult = member.CreateMember
                                    (
                    new Member {
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    PhoneNo   = Input.PhoneNo,
                    Username  = Input.Email,
                    Email     = Input.Email,
                    CreatedAt = DateTime.Now,
                    IsActive  = true,
                    Role      = isAdmin ? "Administrator" : "Normal user"
                });

                if (result.Succeeded && memberResult)
                {
                    if (isAdmin)
                    {
                        bool roleExists = await _roleManager.RoleExistsAsync("Administrator");

                        if (!roleExists)
                        {
                            var role = new IdentityRole();
                            role.Name = "Administrator";
                            await _roleManager.CreateAsync(role);
                        }
                        await _userManager.AddToRoleAsync(user, "Administrator");
                    }

                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    string subject     = "Confirm your email";
                    string htmlContent = $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";
                    if (isAdmin)
                    {
                        subject     = "Confirm " + Input.Email + " as an admin account";
                        htmlContent = $"Please confirm admin's account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";
                        await _emailSender.SendEmailAsync("*****@*****.**", subject, htmlContent);
                    }
                    else
                    {
                        await _emailSender.SendEmailAsync(Input.Email, subject, htmlContent);
                    }

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl, isAdmin = isAdmin }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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