Asp.net website anti-malicious refreshing Cookies and Session Solution

Source: Internet
Author: User

Asp.net website anti-malicious refreshing Cookies and Session Solution

This article describes how to prevent malicious refreshing Cookies and sessions on the asp.net website. It is a very practical technique in WEB programming. Share it with you for your reference. The specific implementation method is as follows:

Session implementation method:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

Public double time;

Public const int freetime = 1; // anti-freezing interval, currently 1 second

# Region Anti-malicious refresh

If (Session. SessionID = null)

{

Response. End ();

}

Else if (Session ["sionid"] = null)

{

Session ["sionid"] = Session. SessionID;

}

If (Session ["last"] = null)

{

Session ["last"] = DateTime. Now;

}

Else

{

DateTime thisTime = DateTime. Now;

DateTime lastTime = DateTime. Parse (Session ["last"]. ToString ());

If (Session. SessionID = Session ["sionid"]. ToString ())

Session ["last"] = thisTime;

TimeSpan ts = thisTime-lastTime;

Time = ts. TotalMilliseconds;

If (time <freetime ** 500)

{

Warm_prompt ();

}

}

# Endregion

Public void warm_prompt ()

{

Response. write ("<table width = '000000' border = '0' align = 'center' cellpadding = '3' cellspacing = '2' bgcolor = '#778 'style = 'font -size: 14px; '> ");

Response. Write ("<tr bgcolor = '# ffff'> ");

Response. Write ("<td> </td> ");

Response. write ("<td bgcolor = '# EEFFEE' to ensure your access security, please" + freetime + "seconds later <a href = '" + Request. rawUrl + "'target = '_ self' style = 'color: # FF0000; '> click here to refresh </a> This page </td> ");

Response. Write ("</tr> ");

Response. Write ("</table> ");

Response. End ();

}

Cookies:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

Public double time;

Public const int freetime = 2;

# Region Anti-malicious refresh

String page;

If (Request. Cookies ["page"] = null)

{

Page = "";

}

Else

{

Page = HttpContext. Current. Request. Cookies ["page"]. Value. ToString (); // obtain the url Value stored in the cookie

}

String strThisPage = HttpContext. Current. Request. Url. PathAndQuery. ToString (); // get the Current page address

DateTime LastTime = DateTime. Now;

If (page. Equals (strThisPage) // if the cookie value is the same as the current page, it indicates a refresh operation.

{

TimeSpan ts = LastTime-DateTime. Parse (HttpContext. Current. Request. Cookies ["time"]. Value. ToString ());

Time = ts. Seconds;

If (time <freetime)

{

Warm_prompt ();

}

}

Else

{

// Perform the operation

Response. Cookies ["page"]. Value = strThisPage;

Response. Cookies ["time"]. Value = LastTime. ToString ();

}

# Endregion

Public void warm_prompt ()

{

Response. write ("<table width = '000000' border = '0' align = 'center' cellpadding = '3' cellspacing = '2' bgcolor = '#778 'style = 'font -size: 14px; '> ");

Response. Write ("<tr bgcolor = '# ffff'> ");

Response. Write ("<td> </td> ");

Response. Write ("<td bgcolor = '# EEFFEE' to ensure your access security, the page will automatically jump to the content you want to access in 2 seconds! </Td> ");

Response. Write ("</tr> ");

Response. Write ("</table> ");

Response. write ("<meta http-equiv = \" refresh \ "content = \" 2 \ "; URL =" + HttpContext. current. request. cookies ["page"]. value. toString () + "> ");

Response. End ();

}

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.