Пример #1
0
        public string DataTest(int id)
        {
            var test      = store.GetTest(id);
            var questions = store.GetQuestionsListByTest(id);

            if (questions.Count() < 1)
            {
                return(null);
            }

            int[] indexes = new int[questions.Count()];
            for (int i = 0; i < indexes.Length; i++)
            {
                indexes[i] = i;
            }
            Random rnd = new Random();

            for (int i = 0; i < indexes.Length; i++)
            {
                int r    = rnd.Next(i, indexes.Length);
                int temp = indexes[i];
                indexes[i] = indexes[r];
                indexes[r] = temp;
            }
            QuestData[] q = new QuestData[test.QuestsCount];
            for (int i = 0; i < q.Length; i++)
            {
                var t = questions.ElementAt(indexes[i % indexes.Length]);
                t.Quest  = Helpers.Helpers.TextWithFormulas(null, t.Quest).ToHtmlString();
                t.Answer = Helpers.Helpers.TextWithFormulas(null, t.Answer).ToHtmlString();
                q[i]     = new QuestData(t.Quest, t.Answer, t.IgnoreCase);
            }
            AjaxObject o = new AjaxObject()
            {
                name          = test.Name,
                priceOfAnswer = test.PriceForQuest,
                duration      = test.Duration,
                questions     = q
            };

            return(JsonSerializer.Serialize(o, typeof(AjaxObject)));
        }
Пример #2
0
        public async Task <IActionResult> AjaxLogin(AjaxLoginInputModel loginInput, string returnUrl = null)
        {
            returnUrl = returnUrl ?? this.Url.Content("~/");

            var ajaxObject = new AjaxObject();

            if (this.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 this.signInManager
                             .PasswordSignInAsync(loginInput.Username, loginInput.Password, loginInput.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    this.logger.LogInformation("User logged in.");
                    ajaxObject.Success = true;
                    ajaxObject.Message = "Logged-in";
                    ajaxObject.Action  = returnUrl;
                }

                if (result.IsLockedOut)
                {
                    this.logger.LogWarning("This account has been locked out, please try again later.");
                    return(this.RedirectToPage("./Lockout"));
                }
                else
                {
                    this.ModelState.AddModelError(string.Empty, "The username or password supplied are incorrect. Please check your spelling and try again.");
                }
            }

            // login was unsuccessful, return model errors
            if (!ajaxObject.Success)
            {
                ajaxObject.Message = ModelErrorsHelper.GetModelErrors(this.ModelState);
            }

            var jsonResult = new JsonResult(ajaxObject);

            return(jsonResult);
        }
Пример #3
0
        public async Task <IActionResult> AjaxRegister(AjaxRegisterInputModel registerInput, string returnUrl = null)
        {
            returnUrl = returnUrl ?? this.Url.Content("~/");

            var ajaxObject = new AjaxObject();

            if (this.ModelState.IsValid)
            {
                Enum.TryParse <Gender>(registerInput.SelectedGender, out Gender gender);

                var user = new CinemaWorldUser
                {
                    UserName = registerInput.Username,
                    Email    = registerInput.Email,
                    FullName = registerInput.FullName,
                    Gender   = gender,
                };

                var result = await this.userManager.CreateAsync(user, registerInput.Password);

                if (result.Succeeded)
                {
                    this.logger.LogInformation("User created a new account with password.");
                    await this.userManager.AddToRoleAsync(user, GlobalConstants.UserRoleName);

                    ajaxObject.Success = true;
                    ajaxObject.Message = "Registered-in";
                    ajaxObject.Action  = returnUrl;

                    var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user);

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

                    await this.emailSender.SendEmailAsync(
                        registerInput.Email,
                        "Confirm your email",
                        htmlMessage : $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (this.userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(this.RedirectToPage("RegisterConfirmation", new { email = registerInput.Email }));
                    }
                    else
                    {
                        await this.signInManager.SignInAsync(user, isPersistent : false);
                    }
                }

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

            if (!ajaxObject.Success)
            {
                ajaxObject.Message = ModelErrorsHelper.GetModelErrors(this.ModelState);
            }

            var jsonResult = new JsonResult(ajaxObject);

            return(jsonResult);
        }