首页 > 解决方案 > 在 POST 调用的情况下用什么代替 LocalRedirect:POST 调用错误

问题描述

在以下处理登录请求的方法中:如果登录成功,我必须在帖子中调用一个页面。post 调用有一个参数,对于调用,我使用标准的 http 方法。

LocalRedirect的语音然后下降。为了使所有工作正常,我应该做什么?

我用 TO DO 指出了代码中的要点。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginViewModel model)
    {
        if (!string.IsNullOrEmpty(ErrorMessage))
        {
            ModelState.AddModelError(string.Empty, ErrorMessage);
        }

        // Se non viene passato un url specifico lo setto sul default
        model.ReturnUrl = model.ReturnUrl ?? Url.Content("/Telemetries/Pagina2");

        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(model.Username, model.Password, model.RememberMe, lockoutOnFailure: true);
            if (result.Succeeded)
            {
                _logger.LogInformation("User logged in.");

                //activity log
                ApplicationUser CurrentUser = await GetCurrentUserAsyncbyUsername(model.Username);
                _activityLogService.InsertActivity(CurrentUser,
                    "LoginUser",
                    "User Logged ({0})",
                    model.Username,
                    CurrentUser.Id);

                List<DeviceSetup> devices = _deviceService.GetList(CurrentUser.CustomerId);

                if (devices!=null && devices.Count!=0)
                {
                    Devices device = _deviceService.GetDeviceByDeviceId(devices[0].DeviceId);


                    // TO DO
                    await WebExtensions.RedirectWithData(device.SerialNo, model.ReturnUrl, HttpContext);

                }
                else
                {
                    model.ReturnUrl = Url.Content("~/");
                    return LocalRedirect(model.ReturnUrl);
                }
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToPage("./LoginWith2fa", new { ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return View(model);
            }
        }

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

POST 调用似乎没有调用该页面。我在被调用页面的控制器中放置了一个断点,但它永远不会在这里停止。对于 post call 我使用这里解释的方法:Response.Redirect with POST 而不是 Get?正如克劳迪奥·卡约·卡斯塔涅蒂(Claudio Cayo Castagnetti)所建议的那样。

提前致谢。

西蒙娜

标签: c#asp.net-corecontroller

解决方案


推荐阅读