コード例 #1
0
        public async Task LinkMemberToFafCommandAsync()
        {
            var member = await Context.Guild.GetMemberAsync(Context.User.Id);

            try
            {
                var token = await _link.StartAsync(Context.Guild.Id, Context.User.Id, Context.User.Username);

                await member.SendMessageAsync($"https://{_configuration["Config:Host"]}/api/link/token/{HttpUtility.HtmlEncode(token)}");
            }
            catch (DiscordIdAlreadyLinkedException)
            {
                var db    = _services.GetRequiredService <QAIDatabaseModel>();
                var guild = await db.FindAsync <DiscordGuildConfiguration>(Context.Guild.Id);

                if (guild is not null &&
                    guild.RoleWhenLinked is not null)
                {
                    if (member.Roles.Any(x => x.Id == guild.RoleWhenLinked.Value))
                    {
                        await Context.ReplyAsync("This Discord account has already been linked, and you already have the assigned role for this server.");
                    }
                    else
                    {
                        var role = Context.Guild.GetRole(guild.RoleWhenLinked.Value);
                        await member.GrantRoleAsync(role);

                        await Context.ReplyAsync($"The role: {role.Name} has been given to your linked account.");
                    }
                }
コード例 #2
0
        public async Task <IActionResult> OnPostAsync(string?returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                if (result is TrooperSignInResult res)
                {
                    if (res.RequiresAccountLinking)
                    {
                        var token = await _link.StartAsync(res.TrooperId, Input.Username, Input.Password, Input.RememberMe);

                        return(Redirect($"/api/link/token/{token}"));
                    }
                    ;
                }
                else
                {
                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User logged in.");
                        return(LocalRedirect(returnUrl));
                    }
                    if (result.RequiresTwoFactor)
                    {
                        return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                    }
                    if (result.IsLockedOut)
                    {
                        _logger.LogWarning("User account locked out.");
                        return(RedirectToPage("./Lockout"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                        return(Page());
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync(string?returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByIdAsync(Input.BirthNumber.ToString());

                if (user is not null)
                {
                    if (user.AccessCode == Input.AccessCode)
                    {
                        var passChangeRes = await _userManager.ChangePasswordAsync(user, user.AccessCode, Input.Password);

                        if (passChangeRes.Succeeded)
                        {
                            user.UserName   = Input.UserName;
                            user.AccessCode = null;
                            var identResult = await _userManager.UpdateAsync(user);

                            if (!identResult.Succeeded)
                            {
                                foreach (var error in identResult.Errors)
                                {
                                    ModelState.AddModelError(error.Code, error.Description);
                                }
                            }
                            else
                            {
                                var res = await _signInManager.PasswordSignInAsync(user, Input.Password, false, false);

                                if (res is TrooperSignInResult result)
                                {
                                    if (result.RequiresAccountLinking)
                                    {
                                        var token = await _link.StartAsync(user.Id, user.UserName, Input.Password, false);

                                        returnUrl = $"/api/link/token/{token}";
                                    }
                                }

                                return(Redirect(returnUrl));
                            }
                        }
                        else
                        {
                            foreach (var error in passChangeRes.Errors)
                            {
                                ModelState.AddModelError(error.Code, error.Description);
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Invalid Access Code", "The inputed Access Code did not match the one saved to this trooper.");
                    }
                }
                else
                {
                    ModelState.AddModelError("Invalid Birth Number", "The inputed Birth Number does not exsist.");
                }
            }

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