Ejemplo n.º 1
0
        public async Task <Object> Register(RegisterUser user)
        {
            if (ModelState.IsValid)
            {
                var registeration = new CustomizeUser()
                {
                    FullName = user.Name,
                    Email    = user.Email,
                    UserName = user.Email
                };
                bool EmailStatus;
                var  code = userManager.GenerateEmailConfirmationTokenAsync(registeration);
                EmailStatus = SendEmail(user.Email, user.Name, code.Result.ToString());
                if (EmailStatus == true)
                {
                    registeration.ConfirmationEmailCode = code.Result.ToString();
                    var result = await userManager.CreateAsync(registeration, user.Password);

                    if (result.Succeeded)
                    {
                        return(Ok(result));
                    }
                    else
                    {
                        string message = "";
                        foreach (var err in result.Errors)
                        {
                            message = err.Code + err.Description;
                        }
                        return(BadRequest(message));
                    }
                }
            }
            return(BadRequest(ModelState));
        }
        public async Task <bool> CheckEmailConfirmCode(string email, string code)
        {
            CustomizeUser user = await _userManager.FindByEmailAsync(email);

            if (user.ConfirmationEmailCode.Equals(code))
            {
                user.EmailConfirmed = true;
                IdentityResult x = await _userManager.UpdateAsync(user);

                return(true);
            }
            return(false);
        }
Ejemplo n.º 3
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(CustomizeUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new CustomizeUser
                {
                    UserName  = Input.Email,
                    Email     = Input.Email,
                    LastName  = Input.LastName,
                    FirstName = Input.FirstName
                };
                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);

                    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);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public async Task <IActionResult> Create(InputModel input)
        {
            if (ModelState.IsValid)
            {
                var user = new CustomizeUser {
                    UserName = input.Email, Email = input.Email
                };
                var createUserResult = await _userManager.CreateAsync(user, input.Password);

                var createUserRoleResult = await _userManager.AddToRoleAsync(user, input.RoleName);

                if (createUserResult.Succeeded && createUserRoleResult.Succeeded)
                {
                    TempData["Success"] = "Created success.";
                    return(RedirectToAction(nameof(Index)));
                }
            }
            TempData["Faile"] = "Email already taken.";
            return(RedirectToAction(nameof(Create)));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new CustomizeUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

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

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }