. NET prevents duplicate submissions

Source: Internet
Author: User

/** prevent pages from being refreshed to cause duplicate submission pages **/

This section is put into a public class to facilitate the use of individual pages
Private readonly string refresh_ticket_name = "__refreshticketarray";
Private readonly string hidden_field_name = "__refreshhiddenfield";
Private readonly string hidden_page_guid = "__refreshpageguid";

<summary>
True to indicate page refresh, false for normal commit
</summary>
public bool Ispagerefreshed
{
Get
{
if (IsPostBack &&!) Checkrefreshflag ())
{

return true;
}
Else
{

return false;
}
}
}

<summary>
Update identity before rendering
</summary>
<param name= "E" ></param>
protected override void OnPreRender (EventArgs e)
{
Log. Debug ("Execute OnPreRender");
Base. OnPreRender (e);
Updaterefreshflag ();
}


<summary>
Update identity, Normal commit deletes the time of the commit, and produces the current new time
</summary>
private void Updaterefreshflag ()
{

#region Cookie Mode

The registration page uniquely identifies and returns
String pageguid = Setcurpageguid ();

HttpCookie cookie = Getrefreshticket ();

if (cookie. Values.count > 0)
{
Cookies. Values.remove (PAGEGUID);
Log. Debug ("The currently purged cookie becomes:" + pageguid);
}

String submittime = DateTime.Now.ToString ("Hhmmss.fffff");
Save current commit time to hidden field
Clientscript.registerhiddenfield (Hidden_field_name, submittime);


Log. Debug ("New Time to be added: Submittime:" + submittime + "Guid:" + pageguid.tostring ());
Cookies. Values.add (Pageguid, submittime);

Log. Debug ("The number of records present in the current cookie in Updaterefreshflag is:" + cookie.) Values.count);
for (int i = 0; i < cookies. Values.count; i++)
Log. Info ("cookie[" + cookie.) Values.getkey (i) + "]:" + cookies. Values[i]);

Response.appendcookie (cookie);

#endregion

}


<summary>
Verifying whether to refresh
</summary>
<returns></returns>
private bool Checkrefreshflag ()
{
HttpCookie cookie = Getrefreshticket ();
String pageguid = Getcurpageguid ();
if (cookie. Values.count > 0)
{
BOOL Flag;
if (cookie. VALUES[PAGEGUID]! = NULL)
Flag = cookie. Values[pageguid]. IndexOf (Getcursubmittime ()) >-1;
Else
Flag = true;//Prevents exceptions and can always be submitted
if (flag)
Log. Debug ("Commit time exists, can be submitted");
Else
Log. Debug ("Invalid commit Time");
return flag;
}
return true;
}


<summary>
Get saved commit time, no new, there is return
</summary>
<returns></returns>
Private HttpCookie Getrefreshticket ()
{
#region Cookie mode, the return value is a cookie

HttpCookie cookies;
if (request.cookies[refresh_ticket_name] = = null)
{
cookie = new HttpCookie (refresh_ticket_name);
Response.appendcookie (cookie);
Log. Debug ("Cookie does not exist, initialize");
}
Else
{
cookie = Request.cookies[refresh_ticket_name];

Log. Debug ("Read the existing cookie, the number of records present in the current cookie is:" + cookie.) Values.count + "specific to the following:");

for (int i = 0; i < cookies. Values.count; i++)
Log. Info ("cookie[" + cookie.) Values.getkey (i) + "]:" + cookies. Values[i]);
}
return cookie;
#endregion
}


<summary>
Get Current commit time
</summary>
<returns></returns>
private String Getcursubmittime ()
{
String submittime = request.params[hidden_field_name] = = null? "": Request.params[hidden_field_name]. ToString ();
Log. Debug ("Execute getcursubmittime:submittime:" + submittime);
return submittime;
}


<summary>
Sets the page's unique identity, using the GUID identifier to differentiate each page's own commit time
</summary>
private String Setcurpageguid ()
{
String GUID;
if (! IsPostBack)
{
if (request.params[hidden_page_guid] = = null)
{
GUID = System.Guid.NewGuid (). ToString ();
Log. Debug ("Setcurpageguid registered a new identity:" + GUID);
}
Else
GUID = Getcurpageguid ();

}
Else
{
GUID = Getcurpageguid ();
}

Clientscript.registerhiddenfield (Hidden_page_guid, GUID);
return GUID;
}



<summary>
Get a unique identifier for the current page
</summary>
<returns></returns>
private String Getcurpageguid ()
{
String pageguid = Request.params[hidden_page_guid] = = null? "None": Request.params[hidden_page_guid]. ToString ();
Log. Debug ("Execute Getcurpageguid () after Page_guid:" + pageguid);
return pageguid;
}
/*/**/used in events that prevent duplicate submissions

if (! ispagerefreshed)
{

}

Else

{

Repeat Commit

}

. NET prevents duplicate submissions

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.