MVC prevents multiple submissions from the customer service side

Source: Internet
Author: User

In the case of web development, there will be multiple clicks on the submit button resulting in multiple submissions, the general centralized approach

1, through JS in the user click on the button disabled off, but this is not very reliable (I can skip this, with a for loop I directly post data)

2, in the generation of customer service HTML when storing a hidden input,input stored in a randomly generated value (usually a GUID), the server will save this value, and so on when the user submits to determine whether the GUID submitted by the match (this method is more than the first security, But if it is a full static page is not satisfied, so I figured out the 3rd solution)

3, through the custom actionfilter to achieve, the code is as follows

public class Preventmutiplesubmitattribute:actionfilterattribute {private int delaysecond = 5;         <summary>/////Repeat Commit time seconds (two times interval is not greater than the change value is treated as duplicate commit), default is 5s//</summary> public int Delaysecond            {get {return delaysecond;}        set {Delaysecond = value;} } public override void OnActionExecuting (ActionExecutingContext filtercontext) {var request=filt            ErContext.RequestContext.HttpContext.Request;            var cache = FilterContext.RequestContext.HttpContext.Cache;            var parammodel = filtercontext.actionparameters;            Parammodel.add ("UserID", ApplicationPrincipal.CurrentPrincipal.UserID); Parammodel.add ("Userip", request.)            userhostaddress);            var Clientpostmodel = Jsonconvert.serializeobject (Parammodel); var Md5model = MD5. Create (). ComputeHash (Encoding.ASCII.GetBytes (Clientpostmodel)).            Select (s = = s.tostring ("x2")); var hashValue= String.            Join ("", Md5model); if (cache[hashvalue] = = null) {cache. ADD (HashValue, "", NULL, DateTime.Now.AddSeconds (Delaysecond), Cache.noslidingexpiration, Cacheitempriority.default                , null); Base.            OnActionExecuting (Filtercontext);            } else {filtercontext.result = new mutiplesubmitexception (); }        }    }

Like a permission filter, this tag is ok where you need to prevent multiple submissions.

Explain the realization of the idea: the main is to submit the user over the parameters + user ip+ user primary key ID and then get the MD5 of these values, the MD5 value will be stored in the cache (only 5 seconds, according to the actual situation can be appropriately increased), when the user submitted to determine whether the cache with this key, If there is a duplicate commit, in the else to do the corresponding processing can be, I mutiplesubmitexception here is a custom exception status code, you can also define your own return JSON can be, with the front-end processing is perfect. OK to this, the server side to prevent multiple submissions completed! If there is a bug thank you user feedback ...

MVC prevents multiple submissions from the customer service side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.