简体   繁体   English

调试时 RedirectToAction 不起作用

[英]RedirectToAction Not Working when Debugging

I have a login form that my project routes to when the user is not authenticated.我有一个登录表单,当用户未通过身份验证时,我的项目会路由到该表单。 Authentication is cached and checked on every action.身份验证被缓存并检查每个操作。 If the user is not authenticated they are routed back to the login screen with a routeUrl passed through the Url.如果用户未通过身份验证,他们将使用通过 Url 的routeUrl路由回登录屏幕。 If the user is not authenticated on the initial load the routeUrl is null and I use RedirectToAction to navigate to the initial post logged in page.如果用户在初始加载时未通过身份验证,则routeUrl为 null,我使用RedirectToAction导航到初始帖子登录页面。

When debugging, my first pass I provide correct credentials and click login.调试时,我第一次通过提供正确的凭据并单击登录。 Stepping through I can verify that the line逐步通过我可以验证该行

return RedirectToAction("Index", "Home", new { Area = "Client" });

is executed.被执行。 But the debugger never hits my breakpoint set on the destination line of the Client/Home/Index controller.但是调试器从未命中我在客户端/主页/索引 controller 的目标行上设置的断点。 I wait and it eventually times out.我等待,它最终超时。

On my second pass with the debugger the authentication is cached and the project routes me straight to the Client/Home/Index with no issue.在我第二次使用调试器时,身份验证被缓存,项目直接将我路由到客户端/主页/索引,没有问题。

When deployed this behavior is different.部署时,此行为是不同的。 The whole login process takes about 90 seconds after the button click for something that shouldn't take more the 5 seconds.单击按钮后,整个登录过程大约需要 90 秒,而这不应该超过 5 秒。 I am trying to debug this but, as stated before, after my Login action authenticates and invokes the redirect, the redirect does not happen.我正在尝试对此进行调试,但如前所述,在我的登录操作验证并调用重定向之后,重定向不会发生。 The browser just sits idle with the spinner spinning and I cannot properly debug.浏览器在微调器旋转时处于空闲状态,我无法正确调试。

Here is the code.这是代码。

[HttpPost]
[AllowAnonymous]
//[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);

    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(model.ReturnUrl);

        case SignInStatus.LockedOut:
            return View("Lockout");

        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe });

        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

The RedirectToLocal function: RedirectToLocal function:

public ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    return RedirectToAction("Index", "Home", new { Area = "Client" });
}

My main Home Controller Index Action that works when redirecting to the same page if the user is already authenticated.我的主要主页 Controller 索引 如果用户已通过身份验证,则在重定向到同一页面时有效。

public ActionResult Index()
{
    return RedirectToAction("Index", "Home", new { Area = "Client" });
}

If you are calling Login() using ajax post.如果您使用 ajax 帖子调用 Login()。 Then the RedirectToAction() will not work you need to return an URL and then use JavaScript to redirect to that location.然后 RedirectToAction() 将不起作用,您需要返回 URL 然后使用 JavaScript 重定向到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM