ASP.NET MCV. How to use TryUpdateMode method instead of Bind attributes to avoid Concurrency conflicts and overposting

ASP.NET MCV. How to use TryUpdateMode method instead of Bind attributes to avoid Concurrency conflicts and overposting

If you use EF (Entity Framework) in your MVC pattern and you are writing new controller to edit data then you usually generate code by scaffolder generator to save time. But generated Edit action method is not recommended anymore in enterprise or business applications because of lack data protection. The generated code has this problem:

Generated by scaffolder action method takes entity field values via input parameters which can be easily provided via HTTP POST by hackers. It means that you need more invest in adding an additional technology to handle protection from possible many generated HTTP post requests by such including the ValidateAntiForgeryToken attribute which helps prevent cross-site request forgery attacks;

Recommended by Microsoft solution:

After generating by scaffolder or when you do it from scratch it is recommended to start use Controller.TryUpdateModel Method. The new code reads the existing entity and calls TryUpdateModel to update fields from user input in the posted form data. 

Look at two screenshots below with bad code and with enhanced code, One important notice is here: As a result of these changes, the method signature of the HttpPost Edit method is the same as the HttpGet edit method; therefore you've renamed the method EditPost. 

Picture 1: Generated by scaffolder Edit method which requires additional investments in future to protect information.



Picture 2: Recommended manually modified Edit method to updated data when source of data is Input Form of your view.


要查看或添加评论,请登录

MIKHAIL LUKYANAU的更多文章

  • WSL2 shrink Ubuntu

    WSL2 shrink Ubuntu

    Someday you have to find free space on your local machine where you actively using dockers for coding, learning and…

  • Development with gen-AI

    Development with gen-AI

    Where and how gen-AI comes to development process. SAP has implemented Joule who is interactive supporter of your…

  • CDS Graphical Modeler

    CDS Graphical Modeler

    Overview Project manager, Business Analyst, Developers (backend, front-end, integrational, of full-stack) in big…

  • Speed up your business using Integration

    Speed up your business using Integration

    How fast you can create flexible process and use it into your applications that should connect customers to your…

  • Anypoint Runtime Fabric vs Standalone Mule Runtime

    Anypoint Runtime Fabric vs Standalone Mule Runtime

    From management perspective using anypoint-cli it is just two different sets of command which do the same things for…

  • How to start with SAP HANA in moment

    How to start with SAP HANA in moment

    If you are going to develop for SAP ecosystem I guess you can save a lot of time if you start to do it by this flow:…

  • We start working with Java9. What we should know about it first.

    We start working with Java9. What we should know about it first.

    As far as you know, the Java 8 has the enhanced functionality of the Interfaces. Before Java 8, interfaces were…

  • Debugging routes in ASP.NET MVC

    Debugging routes in ASP.NET MVC

    I was wondering if it is possible and which outcome of such debugging. You know, that we have actually the list of out…

    1 条评论
  • Fiddler and VS2015 to test ASP.NET projects

    Fiddler and VS2015 to test ASP.NET projects

    Fiddler is the free web debugging proxy for any browser, system or platform. This tool can help you for example test…

  • Your own call-center in 5 minutes

    Your own call-center in 5 minutes

    Your life depends on of communications with colleagues, suppliers, and customers. And you wish to automate the part of…

社区洞察

其他会员也浏览了