zoukankan      html  css  js  c++  java
  • asp.net中限制用户单位时间请求数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace WebApplication1
    {
        
    public class RequestCounter
        {
            
    private static Dictionary<string, RequestCounter> Counters = new Dictionary<string, RequestCounter>();
            
    public const int MaxRequestCount = 3;
            
    public const int ResetSeconds = 2;
            
    public int CurrentCount { get;
                
    private set; }
            
    public string UserID { getset; }
            
    private DateTime LastResetTime;
            
    public bool AddCount()
            {
                DateTime now 
    = DateTime.Now;
                
    if ((now - LastResetTime).Seconds >= ResetSeconds)
                {
                    
    this.CurrentCount = 0;
                    
    this.LastResetTime = now;
                }
           this.CurrentCount++;
                if (this.CurrentCount <= MaxRequestCount)
                    
    return true;
                
    else
                    
    return false;
            }
            
    public static RequestCounter GetCounter(string userID)
            {
                
    if (Counters.ContainsKey(userID))
                    
    return Counters[userID];
                
    else
                {
                    Counters[userID] 
    = new RequestCounter();
                    
    return Counters[userID];
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
        
    public partial class _Default : System.Web.UI.Page
        {
            
    protected void Page_Load(object sender, EventArgs e)
            {
                RequestCounter counter 
    = RequestCounter.GetCounter("xhan");
                
    bool canAccess = counter.AddCount();

                Response.Write(canAccess);
                Response.Write(counter.CurrentCount);

            }
        }
    }


  • 相关阅读:
    [DB2]删除大数据量数据及57011错误处理
    [DB2]DB2日常维护——REORG TABLE命令优化数据库性能
    [转]解读DIV CSS网页布局中CSS无效十个原因
    [DB2]DB2 sqlstate 57016 原因码 "7"错误
    [翻译]15个最常见的WCF问题
    [DB2]DB2数据库备份与恢复和导出表结构与导入导出表数据
    [转]网站(bs系统)怎样实现即时消息思路总结
    【摘抄】DB2字符集问题
    [转]jQuery必知必熟基础知识
    sql 2005/2008 订阅与发布的几个概念
  • 原文地址:https://www.cnblogs.com/xhan/p/1621293.html
Copyright © 2011-2022 走看看