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

            }
        }
    }


  • 相关阅读:
    linux 命令收集
    tomcat + nginx 负载均衡
    lamp + 然之协同
    万能的 命令库
    boost.asio源码剖析(三) 流程分析
    boost.asio源码剖析(一) 前 言
    给你的JAVA程序配置参数(Properties的使用)
    JAVA将Excel中的报表导出为图片格式(三)换一种实现
    JAVA使用apache http组件发送POST请求
    JAVA使用原始HttpURLConnection发送POST数据
  • 原文地址:https://www.cnblogs.com/xhan/p/1621293.html
Copyright © 2020-2023  润新知