public void DeleteSMSCode(SMSBusiness business, string uniqueKey) { var keyByCode = $"{Platform.ToString()}:{SecurityMethod.CellphoneCode.ToString()}:{business.ToString()}:Code:{uniqueKey}"; RedisHelper.KeyDelete(Constant.REDIS_SMS_DBINDEX, keyByCode); }
public void Verify(SMSBusiness business, string uniqueKey, string code, bool deleteCode) { bool isOnlyCellphoneVerify = business != SMSBusiness.SecurityValidate; var securityVerify = new SecurityVerification(Platform); if (isOnlyCellphoneVerify) { securityVerify.CheckErrorCount(business, uniqueKey); } var keyByCode = $"{Platform.ToString()}:{SecurityMethod.CellphoneCode.ToString()}:{business.ToString()}:Code:{uniqueKey}"; var codeInDb = RedisHelper.StringGet(Constant.REDIS_SMS_DBINDEX, keyByCode); if (codeInDb != null && codeInDb == code)//验证通过 { if (deleteCode) { DeleteSMSCode(business, uniqueKey); } else { //如果验证通过,并且不删除这个验证码,表示以后还要用这个验证码验证 RedisHelper.KeyExpire(keyByCode, TimeSpan.FromMinutes(Constant.SMS_EXPIRED_TIME)); } if (isOnlyCellphoneVerify) { securityVerify.DeleteErrorCount(business, uniqueKey); } return; } if (isOnlyCellphoneVerify) { securityVerify.IncreaseErrorCount(business, uniqueKey); } else { securityVerify.IncreaseErrorCount(SecurityMethod.SecurityValidate, uniqueKey, SecurityMethod.CellphoneCode); } }
/// <summary> /// 手机码验证错误次数加1 /// </summary> /// <param name="business"></param> /// <param name="uniqueKey"></param> public void IncreaseErrorCount(SMSBusiness business, string uniqueKey) { SecurityMethod securityMethod = SecurityMethod.CellphoneCode; var errorKey = $"{Platform}:{securityMethod.ToString()}:{business.ToString()}:ErrorCounts:{uniqueKey}"; var errorCountsStr = RedisHelper.StringGet(Constant.REDIS_SMS_DBINDEX, errorKey); int.TryParse(errorCountsStr, out int errorCount); ++errorCount; int spInt = Constant.VIRIFY_FAILD_LOCK_TIME; if (business == SMSBusiness.Register || business == SMSBusiness.UpdateCellphoneNew) { spInt = Constant.REGISTER_FAILD_LOCK_TIME; } RedisHelper.StringSet(Constant.REDIS_SMS_DBINDEX, errorKey, errorCount.ToString(), TimeSpan.FromMinutes(spInt)); if (errorCount >= Constant.VIRIFY_FAILD_TIMES_LIMIT) { var minCount = GetErrorLockTime(Constant.REDIS_SMS_DBINDEX, errorKey); ThrowMoreTimesException(business, minCount); } else { ThrowVerifyFaildException(securityMethod, Constant.VIRIFY_FAILD_TIMES_LIMIT - errorCount); } }
/// <summary> /// 清除手机码验证错误次数 /// </summary> /// <param name="business"></param> /// <param name="uniqueKey"></param> public void DeleteErrorCount(SMSBusiness business, string uniqueKey) { var errorKey = $"{Platform}:{SecurityMethod.CellphoneCode.ToString()}:{business.ToString()}:ErrorCounts:{uniqueKey}"; RedisHelper.KeyDelete(Constant.REDIS_SMS_DBINDEX, errorKey); }
/// <summary> /// 手机码验证错误次数检查 /// </summary> /// <param name="business"></param> /// <param name="uniqueKey"></param> /// <returns></returns> public int CheckErrorCount(SMSBusiness business, string uniqueKey) { SecurityMethod securityMethod = SecurityMethod.CellphoneCode; var errorKey = $"{Platform}:{securityMethod.ToString()}:{business.ToString()}:ErrorCounts:{uniqueKey}"; var errorCountsStr = RedisHelper.StringGet(Constant.REDIS_SMS_DBINDEX, errorKey); int.TryParse(errorCountsStr, out int errorCount); if (errorCount >= Constant.VIRIFY_FAILD_TIMES_LIMIT) { var minCount = GetErrorLockTime(Constant.REDIS_SMS_DBINDEX, errorKey); ThrowMoreTimesException(business, minCount); } return(errorCount); }