Does it make sense to put antiforgerytoken in _Layout.cshtml?

Does it make sense to put antiforgerytoken in _Layout.cshtml?

问题

I'm developing an ASP.NET MVC application and I'm planing to protect each non GET request (POST, PUT, DELETE, etc...) with AntiForegeryToken.

I've implemented an extension of the classical AntiForgery verification based on the [__RequestVerificationToken] sent in the header. This because most of my calls are async ($.ajax()) and it turns out easier for me to send the hidden field value that way.

Does it make sense to put one single @Html.AntiForgeryToken() in the _Layout.cshtml (template for all pages) and always refer to that one only ?

I've tryed to understand what wolud be different beteen this option and putting it in each form (that I don't use much since my requests are pretty much all async), but I haven't.

Can anyone clear this to me ?

Thanks

Lorenzo

 

回答

You can put it in your _Layout.cshtml and generate a single token when the page is rendered, that's fine.

While there is a very slight security benefit of using a different token for every request, if your token has enough entropy (and the standard token generated by @Html.AntiForgeryToken() does), then it is practically infeasible for an attacker to guess the token even during the time of a user session. So one token per user session is still considered secure in most cases.

Actually, trying to use a new token for each request leads to all kinds of bugs in a Javascript heavy application, because the browser needs a non-neglectible time to actually change things like a cookie value or to send a request, and frequent ajax requests will lead to a race condition and you will have hard to debug bugs around token mismatches.

ASP.NET MVC still focuses on traditional form-based applications in this regard, and while you can use it to prevent CSRF in modern Javascript-heavy apps with some tweaks (like a custom attribute to actually verify a token sent in request headers), you do have to write some custom code to do that. Hopefully Microsoft will add built in support in future versions.

UPDATE

After implementing the solution with @Html.AntiForgeryToken() directly in Template page (_Layout.cshtml) I found out a possible problem bound to the use of custom Claims. The problem happens during re-validation of UserIdentity. As a reference I'll leave the link to another post in which I've been dealing with that and added there the wotrkaround for those who choose the same implementation. Custom Claims lost on Identity re validation

Hope it helps !

 

 

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(54)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-09-24 Why would I want to use an ExpressionVisitor?
2019-09-24 Interpreting Expressions
2019-09-24 Traverse an expression tree and extract parameters
2019-09-24 Get Argument Values From Linq Expression
2019-09-24 手机软件
2015-09-24 simplified build configuration
2015-09-24 Visual Studio中的项目属性-->生成-->配置
点击右上角即可分享
微信分享提示