C#ASP.Net MVC:RedirectFromLoginPage始终转到默认URL,而不是returnurl

Rec*_*ipe 9 c# asp.net forms-authentication asp.net-mvc-4

我有一个带会员登录的MVC4应用程序(通过FormsAuthentication).

这在web.config中定义如下.我的默认网址是home root(〜/):

<roleManager enabled="true" />
<authentication mode="Forms">
  <forms defaultUrl="~" loginUrl="~/Account" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

在Login post方法的AccountController中,以下代码是相关的.当用户使用有效凭据单击登录时,将执行此代码.

if (Membership.ValidateUser(creds.Username, creds.Password))
{
    FormsAuthentication.RedirectFromLoginPage(creds.Username, false);
    return null;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我(匿名)导航到:〜/ Admin,我会被重定向到〜/ Account登录,这是完美的.我可以看到网址形成如下:

http://localhost:23759/Account?ReturnUrl=%2fAdmin
Run Code Online (Sandbox Code Playgroud)

但是,当我成功登录时,我被重定向到家(〜/)而不是〜/ Admin

请帮忙!非常感谢!

编辑:发现实际问题:这是post方法没有收到查询字符串

Rec*_*ipe 14

我找到了解决方案!感谢FlopScientist,他让我进一步思考.

确实是因为我正在做一个POST方法,它没有考虑GET中的QueryString .

首先我在我的视图中有这个:

@using (Html.BeginForm("Index", "Account")
{
    <div class="LoginBox">
    //Etc...
    </div>
}
Run Code Online (Sandbox Code Playgroud)

我已将其更新为以下内容:

@using (Html.BeginForm("Index", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{
    //Etc...
}
Run Code Online (Sandbox Code Playgroud)

现在我可以在调试中看到一个查询字符串,我确实得到了正确的重定向!