/// <summary> /// 静态方法,得到使用默认值初始化的ValidateConfig对象. /// </summary> /// <returns>ValidateConfig对象</returns> public static ValidateConfig StaticInitDefaultConfig() { ValidateConfig tempVC = new ValidateConfig(); //背景相关 tempVC.IsBackWaveRandom = false; //字符相关 tempVC.NLen = 4; tempVC.IsCharRandom = true; tempVC.IsUseEnglishChar = true; tempVC.IsUseRandomFont = true; //干扰线相关 tempVC.IsUseRandomLine = true; tempVC.IsUseRandomLineColor = true; tempVC.NLines = 5; tempVC.NLinesWidth = 2; //干扰点相关 tempVC.IsUseRandomPoint = true; tempVC.IsUseRandomPointColor = true; tempVC.PointNum = 20; return(tempVC); }
/// <summary> /// 初始化函数:使用指定路径的配置文件配置验证码.读取顺序为:缓存->物理配置文件->默认值 /// </summary> /// <param name="configFilePath">配置文件路径</param> public void Init(string configFilePath) { rd = new Random((int)System.DateTime.Now.Ticks);//随机数对象 if (configFilePath != null && configFilePath.Trim() != string.Empty) { vConfig = GetValidateCodeConfig(configFilePath); } else { vConfig = InitDefaultConfig(); } SetConfigInfo(vConfig); }
/// <summary> /// 使用配置信息实例设置属性 /// </summary> /// <param name="tempConfig">配置信息类实例</param> private void SetConfigInfo(ValidateConfig tempConfig) { this.IsBackWaveRandom = tempConfig.IsBackWaveRandom; this.NLen = tempConfig.NLen; this.IsCharRandom = tempConfig.IsCharRandom; this.IsUseEnglishChar = tempConfig.IsUseEnglishChar; this.IsUseRandomFont = tempConfig.IsUseRandomFont; this.IsUseRandomLine = tempConfig.IsUseRandomLine; this.IsUseRandomLineColor = tempConfig.IsUseRandomLineColor; this.NLines = tempConfig.NLines; this.NLinesWidth = tempConfig.NLinesWidth; this.IsUseRandomPoint = tempConfig.IsUseRandomPoint; this.IsUseRandomPointColor = tempConfig.IsUseRandomPointColor; this.PointNum = tempConfig.PointNum; }
/// <summary> /// 静态方法,得到配置信息实例 /// </summary> /// <param name="configFilePath">配置文件的路径</param> /// <returns>ValidateConfig对象实例</returns> public static ValidateConfig StaticGetValidateCodeConfig(string configFilePath) { ValidateConfig tempVConfig = new ValidateConfig(); System.Web.Caching.Cache cache = HttpRuntime.Cache;//系统缓存对象 tempVConfig = cache.Get("validateCodeConfig") as ValidateConfig; if (tempVConfig == null) { try { tempVConfig = (ValidateConfig)SerializationHelper.Load(typeof(ValidateConfig), configFilePath); } catch { tempVConfig = StaticInitDefaultConfig();//如果配置文件不存在或者无法反序列化,则使用默认配置. } cache.Insert("validateCodeConfig", tempVConfig); } return(tempVConfig); }
/// <summary> /// 初始化函数:使用默认的配置信息初始化验证码. /// </summary> public void Init() { rd = new Random((int)System.DateTime.Now.Ticks);//随机数对象 vConfig = InitDefaultConfig(); SetConfigInfo(vConfig); }