Why does @Html.AntiForgeryToken() generate different tokens in same response?

后端 未结 6 715
一向
一向 2020-12-16 04:32

A single Razor view contains several forms, each with its own call to @Html.AntiForgeryToken()

@Html.AntiForgeryToken
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 04:49

    I am afraid that won't work.

    The antiforgery token also travels in the response cookie, so yours will contain just the last token, and therefore the first form will always fail.

    You can try to do something like this:

    @{
        ViewBag.Title = "Index";
        var token = Html.AntiForgeryToken();
    }
    
    
        @token 
    
    
    
    @token

    I have tried it, and the same token is used in both forms.

提交回复
热议问题