public byte[] GetVerifyCode() { int codeW = 80; int codeH = 30; int fontSize = 16; string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字体列表,用于验证码 string[] font = { "Times New Roman" }; //验证码的字符集,去掉了一些容易混淆的字符 char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); //生成验证码字符串 for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; } //写入Session、验证码加密 WebHelper.WriteSession("Athena_session_verifycode", Md5.md5(chkCode.ToLower(), 16)); //创建画布 Bitmap bmp = new Bitmap(codeW, codeH); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); //画噪线 for (int i = 0; i < 3; i++) { int x1 = rnd.Next(codeW); int y1 = rnd.Next(codeH); int x2 = rnd.Next(codeW); int y2 = rnd.Next(codeH); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //画验证码字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, fontSize); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0); } //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 MemoryStream ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Png); return(ms.ToArray()); } catch (Exception) { return(null); } finally { g.Dispose(); bmp.Dispose(); } }
public void CheckLogin() { string msg = string.Empty; //检查记录登录次数 if ((this.ctx.Session["PassErrorCount"] != null) && (this.ctx.Session["PassErrorCount"].ToString() != "")) { //获取错误登入次数 int PassErroeCount = Convert.ToInt32(this.ctx.Session["PassErrorCount"]); if (PassErroeCount > 3) { msg = "登录尝试错误次数超过3次"; this.Content(msg); WriteBackHtml(msg, false); return; } } /// 检查验证码 if ((this.ctx.Request["code"] != null) && (this.ctx.Request["code"].ToString() != "")) { if (Md5.md5(ctx.Request["code"].ToString().ToLower(), 16).ToString().ToLower() != this.ctx.Session["Athena_session_verifycode"].ToString().ToLower()) { msg = "验证码填写错误 !"; WriteBackHtml(msg, false); return; } } string userName = this.ctx.Request["username"]; string passWord = this.ctx.Request["password"]; AccountsPrincipal accctx = AccountsPrincipal.ValidateLogin(userName, passWord); string loginpattern = ConfigurationManager.AppSettings["LoginPattern"]; LoginFactory loginfactory = new LoginFactory(); Login lo = loginfactory.GetLoginType(loginpattern); if (lo.IsLoginPatternMessageExist()) { this.ctx.User = accctx; this.ctx.Session["accctx"] = accctx; lo.ClearLoginPattern(); msg = "登录成功,页面跳转中!"; WriteBackHtml(msg, true);//在页面上可以以红字提醒,JS直接direct跳转到下一个链接 } else { if (accctx == null)//登录信息不对 { msg = "登陆失败: " + userName; if ((this.ctx.Session["PassErrorCount"] != null) && (this.ctx.Session["PassErrorCount"].ToString() != "")) { int PassErroeCount = Convert.ToInt32(this.ctx.Session["PassErrorCount"]); this.ctx.Session["PassErrorCount"] = PassErroeCount + 1; } else { this.ctx.Session["PassErrorCount"] = 1; } } else { lo.SetLoginPatternMessage(userName, passWord); //把当前用户对象实例赋给Context.User,这样做将会把完整的用户信息加载到ASP.NET提供的验证体系中 this.ctx.User = accctx; this.ctx.Session["accctx"] = accctx; msg = "登录成功,页面跳转中!"; WriteBackHtml(msg, true);//在页面上可以以红字提醒,JS直接direct跳转到下一个链接 } } }