예제 #1
0
        public async Task <IActionResult> OnPostAsync(CancellationToken ct, string returnUrl = null)
        {
            // returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                PlayBallUser user = new PlayBallUser
                {
                    UserName = Input.Email,
                    Email    = Input.Email,
                };

                var isFirstUser = !await _userManager.Users.AnyAsync(ct);

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

                if (result.Succeeded)
                {
                    _logger.LogInformation("New PlayBallUser created.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        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>.");

                    var addClaimResult = await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "admin"));

                    if (!addClaimResult.Succeeded)
                    {
                        // TODO: somthing went wrong, handle it!
                    }



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

                    if (string.IsNullOrWhiteSpace(returnUrl))
                    {
                        return(LocalRedirect("~/"));
                    }
                    else
                    {
                        return(Redirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(Page());
        }
예제 #2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(PlayBallUser 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);

            QrCode = _qrCodeGenerator.Generate(GenerateQrCodeUri(email, unformattedKey));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            //returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new PlayBallUser
                {
                    UserName = Input.Email,
                    Email    = Input.Email,
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("new User ceated.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbakUrl = Url.Page("/ConfirmEmail",
                                              null, new { userId = user.Id, code = code },
                                              Request.Scheme);
                    await _emailSender.SendEmailAsync(Input.Email, "Confirm Email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbakUrl)}'>Clicing here </a>");

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

                    if (string.IsNullOrWhiteSpace(returnUrl))
                    {
                        return(LocalRedirect("~/"));
                    }
                    else
                    {
                        return(Redirect(returnUrl));
                    }
                }

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