ASP.NET MVC HttpPost和SignOn()混淆

时间:2011-08-02 16:52:45

标签: .net asp.net-mvc authentication membership membership-provider

据我所知 [HttpPost] 属性或任何POST方法,在状态发生变化时使用它。但是,如果使用loginUrl设置Forms身份验证,请执行以下操作:

<forms loginUrl="~/Account/LogIn" ...

这会在遇到[Authorize]属性时强制重定向。例如:

[Authorize]
public ActionResult AccessPrivateData()
{
    // Should redirect to /Account/LogIn if AuthCookie not set
    // ...
}

到目前为止一切顺利。我的问题是我现在无法使用[HttpPost]进行LogIn操作(因为你无法重定向到POST):

[HttpPost]
public ActionResult LogIn(string username, string password)
{
    // Won't find the URL (/Account/LogIn) if redirected to here...
    // ...
}

但是LogIn操作确实不会改变状态,保证POST吗?请有人提供一些解释,如果可以的话,你如何处理这个问题。

1 个答案:

答案 0 :(得分:2)

您可以有两个LogIn操作。重定向将使用GET并被发送到简单呈现登录表单的操作。

发布表单时,它将使用[HttpPost]

修饰的方法
[HttpGet]
public ActionResult Login()
{
  // Render view
}

[HttpPost]
public ActionResult LogIn(string username, string password)
{
  // Process form post
}