无法解释为什么在使用RedirectFromLogin登录后不重定向

时间:2010-01-29 14:43:36

标签: c# asp.net asp.net-mvc forms-authentication

我正在使用ASP.NET MVC,我正在做的登录操作:

[AcceptVerbs("POST")]
public ActionResult Login(FormCollection form)
{
    User validatedUser = // tests username/pwd here.

    FormsAuthentication.RedirectFromLoginPage(
        validatedUser.ID.ToString(), rememberMe);

    if(String.IsNullOrEmpty(Request["ReturnUrl"]))
        string redirectUrl = Request["ReturnUrl"];

    if (!String.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
        string redirectUrl = Request["ReturnUrl"];
}

当我在登录页面时,我的网址看起来像这样:

http://localhost:56112/user/login?ReturnUrl=/admin/settings

这里有什么不妥吗?

我的web.config:

<authentication mode="Forms">
    <forms loginUrl="/user/login"
        protection="All"
        timeout="30"
        name="SomeCookie"
        requireSSL="false"
        slidingExpiration="true"
        defaultUrl="default.aspx" />

1 个答案:

答案 0 :(得分:6)

我建议您不要在MVC应用程序中使用RedirectFromLoginPage方法。使用标准ASP.NET MVC技术执行重定向会更好。您可以使用SetAuthCookie方法:

public ActionResult Login(FormCollection form)
{
    // ...
    FormsAuthentication.SetAuthCookie(validatedUser.ID.ToString(), rememberMe);
    // ...
    return Redirect(FormsAuthentication.DefaultUrl); // will read default url from web.config
}