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);

            }
        }
    }


  • 相关阅读:
    概率论——随机事件及其概率
    Web应用程序项目以配置使用IIS。未找到Web服务器”链接地址”
    LaTeX中.sty文件缺失解决办法
    IIS中的经典模式和集成模式有什么区别
    判断有序整型数组中是否存在两数,相加之和等于给定的任意整数
    51Job的搜索技巧
    登录失败。该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。
    Ubuntu使用Latex模板moderncv写简历
    COM相关操作(C#)
    什么是单线程单元(STA)什么是多线程单元(MTA)
  • 原文地址:https://www.cnblogs.com/xhan/p/1621293.html
Copyright © 2011-2022 走看看