代码之家  ›  专栏  ›  技术社区  ›  Bryan Dellinger

在车把模板中添加防伪标识

  •  1
  • Bryan Dellinger  · 技术社区  · 6 年前

    我已经改变了剃须刀的看法,以一个车把模板。

     using (Html.BeginForm("Start", "Form", FormMethod.Post, new { formTypeId = @Model.TypeId, organizationId = @Model.OrganizationId }))
     {
         @Html.AntiForgeryToken()
         <input type="hidden" name="formTypeId" value="@Model.TypeId" />
         <input type="hidden" name="organizationId" value="@Model.OrganizationId" />
         <button class="btn btn-primary btn-block" type="submit">Start</button>
     }
    

    在车把模板中:

    <form action="{{StartLink}}" method="post">
        <input type="hidden" name="formTypeId" value="{{TypeId}}" />
        <input type="hidden" name="organizationId" value="{{OrganizationId}}" />
        <button class="btn btn-primary btn-block" style="margin-bottom: 5px;" type="submit">Start</button>
    </form>
    

    我不知道如何添加防伪令牌的把手形式。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Bryan Dellinger    6 年前

    好吧,这就是我的想法。

    在我的索引.cshtml我将一个反伪造令牌传递给创建handlebars模板的js。

    @section scripts {
        <script>
            $(function () {
                window.formListBuilder = new app.components.FormListBuilder({
                    container: '#forms-container',
                    baseUrl: '@baseUrl.ToString()',
                    antiForgeryToken: '@Html.AntiForgeryToken()',
                    currentUser: JSON.parse('@Html.Raw(Json.Serialize(currentUser))')
                })
            });
    
        </script>
    }
    

    然后在车把模板中我使用了防伪标识。

    <form action="{{StartLink}}" method="post">
                        {{{antiForgeryToken}}}
                        <input type="hidden" name="formTypeId" value="{{TypeId}}" />
                        <input type="hidden" name="organizationId" value="{{OrganizationId}}" />
                        <button class="btn btn-primary btn-block" style="margin-bottom: 5px;" type="submit">Start</button>
                    </form>
    

    这是一个可接受的解决方案还是以某种方式绕过了安全性?