我有一个具有成员(member)登录功能的 MVC4 应用程序(通过 FormsAuthentication)。
这在 web.config 中定义如下。 我的默认 url 是主目录 (~/):
<roleManager enabled="true" />
<authentication mode="Forms">
<forms defaultUrl="~" loginUrl="~/Account" />
</authentication>
在登录后方法中的我的 AccountController 中,以下代码是相关的。 当用户使用有效凭据单击登录时,将执行此代码。
if (Membership.ValidateUser(creds.Username, creds.Password))
{
FormsAuthentication.RedirectFromLoginPage(creds.Username, false);
return null;
}
现在,如果我导航(匿名)到:~/Admin,我将被重定向到 ~/Account 以登录,这是完美的。我可以看到 url 的构成如下:
http://localhost:23759/Account?ReturnUrl=%2fAdmin
但是,当我成功登录时,我被重定向到主目录 (~/) 而不是 ~/Admin
求助! 非常感谢!
编辑:发现实际问题:是 post 方法没有收到查询字符串
最佳答案
我找到了解决方案! 感谢 FlopScientist,他让我进一步思考。
确实是因为我在做一个POST方法,没有考虑到GET中的QueryString。
首先我在我的 View 中有这个:
@using (Html.BeginForm("Index", "Account")
{
<div class="LoginBox">
//Etc...
</div>
}
我已将其更新为以下内容:
@using (Html.BeginForm("Index", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{
//Etc...
}
现在我实际上可以在我的调试中看到一个查询字符串,并且我确实得到了正确的重定向!
关于C# ASP.Net MVC : RedirectFromLoginPage always goes to default url, 并且不返回 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18790777/