ASP.NET:在一般处理程序中通过 Session 保存验证码却无法显示图片?

 1 using System.Drawing;
 2 using System.Web;
 3 using System.Web.SessionState;
 4 
 5 /// <summary>
 6 /// CaptchaHandler 的摘要说明
 7 /// </summary>
 8 public class CaptchaHandler : IHttpHandler, IRequiresSessionState  //简记:我需要Session
 9 {
10 
11   public void ProcessRequest(HttpContext context)
12   {
13 
14     // GDI+ 三步 1画布 2为画布创建画笔 3绘制所需素材
15 
16     var vCode = CaptchaHelper.CreateRandomCode(5);  //自己封装的方法
17 
18     var buffer = CaptchaHelper.DrawImage(vCode, background: Color.White);  //自己封装的方法
19     context.Session["vCode"] = vCode;  //vCode:string 类型的验证码字符串
20 
21     context.Response.ContentType = "image/gif";
22     context.Response.BinaryWrite(buffer);
23   }
24 
25   public bool IsReusable { get { return false; } }
26 }

在一般处理程序中如果要使用Session:

【关键】Handler 要实现 IRequiresSessionState 接口(所在的命名空间 using System.Web.SessionState;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET短信登录验证码图片验证程序源码可以使用以下代码实现。 短信验证码部分: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Net.Mail; using System.Web.Services; public partial class SMSVerification : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static bool GenerateCode(string phone) { // 生成随机的4位验证码 Random rand = new Random(); int code = rand.Next(1000, 9999); // 发送验证码到手机号码 MailMessage message = new MailMessage(); SmtpClient smtpClient = new SmtpClient("smtp.gmail.com"); smtpClient.EnableSsl = true; message.From = new MailAddress("your-email@gmail.com"); message.To.Add(phone); message.Subject = "登录验证码"; message.Body = "验证码:" + code.ToString(); smtpClient.Send(message); return true; } } ``` 图片验证部分: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ImageVerification : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GenerateImageCode(); } } protected void btnSubmit_Click(object sender, EventArgs e) { if (txtCode.Text.Equals(Session["Code"].ToString(), StringComparison.OrdinalIgnoreCase)) { lblResult.Text = "验证码正确"; } else { lblResult.Text = "验证码错误"; GenerateImageCode(); } } private void GenerateImageCode() { string code = Guid.NewGuid().ToString().Substring(0, 6); Session["Code"] = code; Response.Clear(); Response.ContentType = "image/jpeg"; using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 30)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.FillRectangle(System.Drawing.Brushes.White, 0, 0, 100, 30); g.DrawString(code, new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 16), System.Drawing.Brushes.Black, 2, 2); } bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } Response.End(); } } ``` 以上是一个简单的ASP.NET短信登录验证码图片验证程序的源码示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值