【问题标题】:MVC5 Web Application Scaffold - Account/Logoff - Why use HTTP Post?MVC5 Web 应用程序脚手架 - 帐户/注销 - 为什么使用 HTTP Post?
【发布时间】:2015-08-05 11:52:48
【问题描述】:

我正在尝试了解 MVC 5 Web 应用程序模板,我注意到对 LogOff 链接周围的安全性给予了特别关注。

在脚手架模板中,_LoginPartial.cshtml 视图中的“LogOff”链接位于带有 AntiForgeryToken 的 HTML 表单内,并被定义为对表单提交操作的 JS 调用,如下所示:

@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}

在 ActionController 中对应的动作方法 Account/LogOff 定义如下:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

我的问题是 - 它背后的原因是什么?为什么注销操作需要如此多的安全保护?为什么不在视图中显示这个,

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
@Html.ActionLink("Log Off", "LogOff", "Account", routeValues: null, htmlAttributes: new { title = "LogOff" })

这在控制器中:

 public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

这会造成什么安全漏洞?

谢谢。

【问题讨论】:

    标签: asp.net-mvc security asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    请参考此链接:Logout: GET or POST?

    它将回答您关于为什么应在注销时使用 Post 的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-08
      • 2021-10-02
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      相关资源
      最近更新 更多