【问题标题】:Simple counter increment in Asp.net CoreAsp.net Core 中的简单计数器增量
【发布时间】:2020-08-20 07:00:39
【问题描述】:

在我的控制器中,我想计算错误登录尝试的次数。

else
{
     // Increment counter by 1
     // check if counter == 3
     // ban user
     logonAttempt++;
     if (logonAttempt >= MAX_LOGON_ATTEMPT)
     {
          ModelState.AddModelError("", "This account has been locked. Please contact the help desk for further support.");
     } else
     {
          ModelState.AddModelError("", "You have entered an invalid username or password.");
     }
}

每次用户点击“提交”并且代码进入这个 else 语句时,logonAttempt 重置为 0。

有没有办法阻止它重置?用户会话?

【问题讨论】:

  • 您是否将 loginAttempt 变量存储回会话中?
  • 或者你真的在使用会话吗?
  • 我相信我是。如果用户登录成功,等待 HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
  • 您需要使用 HttpContext.Session 来存储多个请求的任何值,但@aepelman 的帖子对于身份验证是正确的

标签: c# asp.net asp.net-core-mvc counter


【解决方案1】:

我认为你误解了一些东西。 您可以计入会话,但新的架构模式往往以更少的会话工作。 生命周期应该是请求、实例化非常小的东西、响应,然后释放实例以开始轻量级工作。

您可以做的一件事就是使用您用于登录的验证令牌或方法更新存储的 cookie。 我的建议是检查这个框架,它非常灵活,所有这些问题都已经解决了。

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio

【讨论】:

    【解决方案2】:

    也许你可以这样做:

    // 我的班级

    private static XDic<DateTime, XDic<string, int>> daily_reg_attemps = new XDic<DateTime,XDic<string, int>>(); 
    const int limit = 20;
    
        public bool is_banned(string key,HttpRequestMessage request)
        {
            //handle attempt monitoring
            var today = DateTime.Today.Date;
            if (!daily_reg_attemps.ContainsKey(today))
                daily_reg_attemps.Add(today, new XDic<string, int>());
            var reg_attemps = daily_reg_attemps[today];
    
            //handle attempts
            var ip = GetClientIp(request);
            if (!reg_attemps.ContainsKey(ip)) reg_attemps.Add(ip, 0);
            if (!reg_attemps.ContainsKey(key)) reg_attemps.Add(key, 0);
    
            //prevent localhost
            if (!request.RequestUri.Host.Contains("localhost"))
            {
                reg_attemps[ip]++;
                reg_attemps[key]++;
            }
    
            return (reg_attemps[ip] > limit || reg_attemps[key] > limit);
        }
    

    //我的自定义词典

    public class XDic<TKey, TValue> : Dictionary<TKey, TValue>
    {
       public virtual void Add(TKey key, TValue value, bool updateIfExist = true)
        {
            if (key == null)
            {
                throw new ArgumentException("Key parameter is Null.");
            }
    
            if (base.ContainsKey(key))
            {
                if (updateIfExist)
                {
                    base[key] = value;
                }
                else {
                    throw new ArgumentException("Error")
                }
            }
            else {
                base.Add(key, value);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 2013-11-20
      • 2011-07-31
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多