/// <summary> /// 取得运算随机字符串,并设置Session值 /// </summary> public static void IdentifyCode2() { string[] number1 = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; string[] number2 = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] operate = new string[] { "加", "减" }; string[] equals = new string[] { "=", "等于" }; IList <string[]> number = new List <string[]>(); number.Add(number1); number.Add(number2); Random r = new Random(); int num1 = r.Next(1, 11); int num2 = r.Next(1, 11); int result = 0; int operation = r.Next(0, 2); if (operation == 0) { result = num1 + num2; } else { if (num1 < num2) { int num3 = 0; num3 = num1; num1 = num2; num2 = num3; } result = num1 - num2; } string checkCode = string.Empty; checkCode = ((string[])number[r.Next(0, 2)])[num1 - 1]; checkCode += operate[operation]; checkCode += ((string[])number[r.Next(0, 2)])[num2 - 1]; checkCode += equals[r.Next(0, 2)]; checkCode += "?"; Cookie2.Set("IdentifyCode", result.ToString()); DrawIdentifyCode2(checkCode); }
//#endregion //#region IdentifyCode /// <summary> /// 取得随机字符串,并设置Session值 /// </summary> /// <example> /// <code> /// string strIdentifyCode = Pub.Class.Identify.IdentifyCode(4); /// Pub.Class.Identify.DrawIdentifyCode(strIdentifyCode, 50, 100); /// Response.End(); /// 在登录页面引用此文件:<img src="Valid.aspx" border="0" /> /// Session["IdentifyCode"] /// </code> /// </example> /// <param name="intLength">长</param> /// <returns>取得随机字符串</returns> public static string IdentifyCode(int intLength) { int intNumber; char chrCode; string strIdentifyCode = String.Empty; Random rndRandom = new Random(); for (int i = 0; i < intLength; i++) { intNumber = rndRandom.Next(); if (intNumber % 2 == 0) { chrCode = (char)('0' + (char)(intNumber % 10));//如果随机数是偶数 取余 } else { chrCode = (char)('A' + (char)(intNumber % 26));//如果随机数是奇数 选择从[A-Z] } strIdentifyCode += chrCode.ToString(); } Cookie2.Set("IdentifyCode", strIdentifyCode); return(strIdentifyCode); }
//#endregion //#region COOKIES防刷新页面代码 /// <summary> /// 设置打开页面的时间 /// </summary> public static void SetDateTime() { Cookie2.Set("__sysTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }