发布于 2016-06-13 04:15:01 | 90 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


这篇文章主要介绍了asp.net网站防恶意刷新的Cookies与Session解决方法,分别以实例的形式讲述了采用cookie法与session法实现WEB页面防止恶意刷新的技巧,需要的朋友可以参考下

本文实例讲述了asp.net网站防恶意刷新的Cookies与Session解决方法,是WEB程序设计中非常实用的技巧。分享给大家供大家参考。具体实现方法如下:

Session版实现方法:


public double time;
public const int freetime = 1;//防刷冰冻时间间隔,当前为1秒

#region 防恶意刷新
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='778' border='0' align='center' cellpadding='3' cellspacing='2' bgcolor='#009900' style='font-size: 14px; '>");
    Response.Write(" <tr bgcolor='#FFFFFF'>");
    Response.Write("  <td><img src='/newimages/logos.gif'></td>");
    Response.Write("  <td bgcolor='#EEFFEE'为了保证您的访问安全,请您 " + freetime + " 秒后<a href='" + Request.RawUrl + "' target='_self' style='color:#FF0000;'>点击这里刷新</a>此页面</td>");
    Response.Write(" </tr>");
    Response.Write("</table>");
    Response.End();
}

Cookies版实现方法:


public double time;
public const int freetime = 2;

#region 防恶意刷新
string page;
if (Request.Cookies["page"] == null)
{
  page = "";
}
else
{
  page = HttpContext.Current.Request.Cookies["page"].Value.ToString(); //获取cookie中存储的url值 
}

string strThisPage = HttpContext.Current.Request.Url.PathAndQuery.ToString();//获取当前页地址 
DateTime LastTime = DateTime.Now;
if (page.Equals(strThisPage))//如果cookie中的值和当前页相等,那么表示是刷新操作 
{
  TimeSpan ts = LastTime - DateTime.Parse(HttpContext.Current.Request.Cookies["time"].Value.ToString());

  time = ts.Seconds;
  if (time < freetime)
  {
 warm_prompt();
  }
}
else
{
  //执行操作 
  Response.Cookies["page"].Value = strThisPage;
  Response.Cookies["time"].Value = LastTime.ToString();
}
#endregion

public void warm_prompt()
{
    Response.Write("<table width='778' border='0' align='center' cellpadding='3' cellspacing='2' bgcolor='#009900' style='font-size: 14px; '>");
    Response.Write(" <tr bgcolor='#FFFFFF'>");
    Response.Write("  <td><img src='/newimages/logos.gif'></td>");
    Response.Write("  <td bgcolor='#EEFFEE'为了保证您的访问安全,页面将在2秒后将自动跳转到您要访问的内容!</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();
}

相信本文所述对大家的asp.net程序设计有一定的借鉴价值。



最新网友评论  共有(1)条评论 发布评论 返回顶部
doaz 发布于2016-08-03 21:50:54
解决,赞
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务