Пример #1
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(RadialUser 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);
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                if (Input.Username.ToLower().Contains("system") ||
                    Input.Username.ToLower().Contains("admin"))
                {
                    ModelState.AddModelError("Input.Username", "Invalid username.");
                    return(Page());
                }

                if (await _userManager.FindByEmailAsync(Input.Email) != null)
                {
                    ModelState.AddModelError("Input.Email", "Email address is already in use.");
                    return(Page());
                }

                var user = new RadialUser
                {
                    UserName = Input.Username,
                    Email    = Input.Email,
                };

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

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

                    var character = new PlayerCharacter()
                    {
                        CorePower     = 20,
                        EnergyCurrent = 20,
                        Name          = Input.Username,
                        Type          = Enums.CharacterType.Player,
                        UserId        = user.Id
                    };

                    _world.CharacterBackups.AddOrUpdate(character.Name, character);
                    await _world.Save();

                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));

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

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

                    if (!emailResult)
                    {
                        await _userManager.ConfirmEmailAsync(user, token);
                    }

                    if (emailResult && _userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    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());
        }