ASP.NET MVC HttpPost 和 SignOn() 混淆
据我了解 [HttpPost] 属性或任何与此相关的 POST 方法,它在状态更改时使用。但是,如果您使用如下的登录 URL 设置表单身份验证:
<forms loginUrl="~/Account/LogIn" ...
这将在遇到 [Authorize] 属性时强制重定向。示例:
[Authorize]
public ActionResult AccessPrivateData()
{
// Should redirect to /Account/LogIn if AuthCookie not set
// ...
}
到目前为止一切顺利。我的问题是,我现在无法使用 [HttpPost] 进行登录操作(因为您无法重定向到 POST):
[HttpPost]
public ActionResult LogIn(string username, string password)
{
// Won't find the URL (/Account/LogIn) if redirected to here...
// ...
}
但是登录操作是否确实会更改状态,从而保证 POST?请有人提供一些解释,如果可以的话,请提供如何处理这个问题。
As I understand the [HttpPost] attribute, or any POST method for that matter, it is used when state is changed. However, if you set up Forms Authentication with a loginUrl like:
<forms loginUrl="~/Account/LogIn" ...
this will force a redirect when an [Authorize] attribute is encountered. Example:
[Authorize]
public ActionResult AccessPrivateData()
{
// Should redirect to /Account/LogIn if AuthCookie not set
// ...
}
So far so good. My problem is that I can't use [HttpPost] for the LogIn action now (because you can't redirect to a POST):
[HttpPost]
public ActionResult LogIn(string username, string password)
{
// Won't find the URL (/Account/LogIn) if redirected to here...
// ...
}
but wouldn't a LogIn action indeed change state, warranting a POST? Please someone offer some explanation, and if you can, how you deal with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以有两个登录操作。重定向将使用 GET 并发送到仅呈现登录表单的操作。
当表单被发布时,它将使用用
[HttpPost]
修饰的方法You could have two LogIn actions. The redirect will use a GET and get sent to the action that simply renders the login form.
When the form is posted, it will use the method decorated with
[HttpPost]