Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var userList = new List <PcrIdentityUser>();

            using (var sr = new StringReader(Input))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var qq    = ulong.Parse(line);
                    var name  = qq.ToString();
                    var email = $"{qq}@qq.com";
                    if (await _context.DbContext.Users.AnyAsync(u => u.QQID == qq || u.Email == email || u.UserName == name))
                    {
                        StatusMessage2 = $"´íÎó£ºQQ {qq} ÒѾ­×¢²á¡£";
                        return(Page());
                    }
                    var user = new PcrIdentityUser
                    {
                        QQID     = qq,
                        UserName = name,
                        Email    = email,
                        GuildID  = guild.GuildID,
                    };
                    userList.Add(user);
                    //Not creating with UserManager. This allows to create without saving.
                    _context.DbContext.Users.Add(user);
                }
            }
            await _context.DbContext.SaveChangesAsync();

            if (!string.IsNullOrEmpty(Password))
            {
                foreach (var u in userList)
                {
                    await _userManager.AddPasswordAsync(u, Password);
                }
            }

            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            foreach (var u in userList)
            {
                imGuild.AddUser(u.Id, 0, null);
            }

            StatusMessage = "Ìí¼ÓÍê³É¡£";
            return(RedirectToPage("/Guilds/Edit"));
        }
        private async Task LoadAsync(PcrIdentityUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            Username = userName;

            Input = new InputModel
            {
                GameID           = user.GameID,
                QQID             = user.QQID == 0 ? null : user.QQID,
                DisableYobotSync = user.DisableYobotSync,
            };
        }
        private async Task LoadAsync(PcrIdentityUser user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
Example #4
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(PcrIdentityUser 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);
        }
Example #5
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new PcrIdentityUser {
                    QQID = Input.QQ, UserName = Input.QQ.ToString(), Email = $"{Input.QQ}@qq.com"
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    if (Input.QQ == AdminQQ)
                    {
                        if (!await _roleManager.RoleExistsAsync("Admin"))
                        {
                            var role = new IdentityRole
                            {
                                Name = "Admin"
                            };
                            await _roleManager.CreateAsync(role);
                        }
                        result = await _userManager.AddToRoleAsync(user, "Admin");
                    }
                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created a new account with password.");

                        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());
        }
Example #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 PcrIdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };

                var result = await _userManager.CreateAsync(user);

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

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                        var userId = await _userManager.GetUserIdAsync(user);

                        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 = userId, 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>.");

                        // If account confirmation is required, we need to show the link if we don't have a real email sender
                        if (_userManager.Options.SignIn.RequireConfirmedAccount)
                        {
                            return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email }));
                        }

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

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

            ProviderDisplayName = info.ProviderDisplayName;
            ReturnUrl           = returnUrl;
            return(Page());
        }
Example #7
0
        public async Task <IActionResult> OnPostAsync()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var currentCount = await _context.DbContext.Users
                               .Where(u => u.GuildID == guild.GuildID)
                               .CountAsync();

            if (CloneCount <= 0)
            {
                StatusMessage2 = "错误:克隆数量无效。";
                return(Page());
            }
            if (CloneCount + currentCount > 30)
            {
                StatusMessage2 = "错误:公会人数不得超过30人。";
                return(Page());
            }

            var templateUser = await _context.DbContext.Users
                               .FirstOrDefaultAsync(u => u.QQID == TemplateUserQQ);

            if (templateUser is null)
            {
                StatusMessage2 = "错误:模板用户不存在。";
                return(Page());
            }
            if (templateUser.GuildID != guild.GuildID)
            {
                StatusMessage2 = "错误:模板用户不在本公会。";
                return(Page());
            }

            const string EmailFormat = "clone_{0}_{1}@example.com";
            var          cloneTime   = DateTime.UtcNow.Ticks.ToString();

            var templateUserConfigs = await _context.DbContext.UserCharacterConfigs
                                      .Where(cc => cc.UserID == templateUser.Id).ToListAsync();

            var newUserList = new List <PcrIdentityUser>();

            for (int i = 0; i < CloneCount; ++i)
            {
                var email = string.Format(EmailFormat, cloneTime, i).ToUpperInvariant();
                var user  = new PcrIdentityUser()
                {
                    Email          = null,
                    UserName       = email,
                    GameID         = templateUser.GameID + "_" + (i + 1).ToString(),
                    EmailConfirmed = true,
                    GuildID        = guild.GuildID,
                };
                newUserList.Add(user);
                if (await _context.DbContext.Users.AnyAsync(u => u.NormalizedEmail == email))
                {
                    StatusMessage2 = $"错误:用户{email}已存在。请重新尝试。";
                    return(Page());
                }
            }

            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            for (int i = 0; i < CloneCount; ++i)
            {
                _context.DbContext.Users.Add(newUserList[i]);
                {
                    //Clone configs.
                    foreach (var cc in templateUserConfigs)
                    {
                        _context.DbContext.UserCharacterConfigs.Add(new()
                        {
                            User = newUserList[i],
                            CharacterConfigID = cc.CharacterConfigID,
                        });
                    }
                }
                newUserList.Add(newUserList[i]);
            }

            await _context.DbContext.SaveChangesAsync();

            foreach (var user in newUserList)
            {
                imGuild.AddUser(user.Id, 0, templateUser.Id);
            }

            StatusMessage = "克隆完成。";
            return(RedirectToPage("/Guilds/Edit"));
        }