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

            }
        }
    }


  • 相关阅读:
    uestc Can You Help God Wu
    uestc 方老师开橙卡
    【JS】【24】监听鼠标滚轮事件
    【Java】【26】截取字符串
    【HTML&CSS】【5】点击号码可以调用手机拨号功能
    【JS】【23】on()绑定事件和off()解除绑定事件
    【Eclipse】【5】FreeMarker插件
    【Oracle】【18】获取数据库当前用户下所有表名和表名的注释
    【其他】【PL/SQL Developer】【2】报错Initialization error Could not load ".../oci.dll"解决方法
    【JDK】【1】下载官方版本
  • 原文地址:https://www.cnblogs.com/xhan/p/1621293.html
Copyright © 2011-2022 走看看