C# 会话未从 HTTPHandler 保存
我有一个 HTTPHandler,每当请求 captcha.ashx 页面时,它都会向用户生成验证码图像。它的代码很简单:
CaptchaHandler handler = new CaptchaHandler();
Random random = new Random();
string[] fonts = new string[4] { "Arial", "Verdana", "Georgia", "Century Schoolbook" };
string code = Guid.NewGuid().ToString().Substring(0, 5);
context.Session.Add("Captcha", code);
Bitmap imageFile = handler.GenerateImage(code, 100, 70, fonts[random.Next(0,4)]);
MemoryStream ms = new MemoryStream();
imageFile.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] buffer = ms.ToArray();
context.Response.ClearContent();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(buffer);
context.Response.Flush();
然后在我的常规网站上,我得到以下内容:
...
<img id="securityCode" src="captcha.ashx" alt="" /><br />
<a href="javascript:void(0);" onclick="javascript:refreshCode();">Refresh</a>
...
这工作完美,每当请求 captcha.ashx 页面时,都会生成图像并将其发送回用户。我的问题是 HTTPHandler 不保存会话? 我试图从正常页面恢复会话,但只得到一个异常,说它不存在,所以我打开跟踪来查看哪些会话处于活动状态,并且它没有列出 HTTPHandler 创建的会话(验证码)。
HTTPHandler 使用 IReadOnlySessionState 与会话交互。 HTTPHandler 是否只有读取访问权限,因此不存储会话?
I have a HTTPHandler that produces a Captcha image to the user whenever the captcha.ashx page is requested. The code for it is simple:
CaptchaHandler handler = new CaptchaHandler();
Random random = new Random();
string[] fonts = new string[4] { "Arial", "Verdana", "Georgia", "Century Schoolbook" };
string code = Guid.NewGuid().ToString().Substring(0, 5);
context.Session.Add("Captcha", code);
Bitmap imageFile = handler.GenerateImage(code, 100, 70, fonts[random.Next(0,4)]);
MemoryStream ms = new MemoryStream();
imageFile.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] buffer = ms.ToArray();
context.Response.ClearContent();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(buffer);
context.Response.Flush();
Then on my regular website, i got the following:
...
<img id="securityCode" src="captcha.ashx" alt="" /><br />
<a href="javascript:void(0);" onclick="javascript:refreshCode();">Refresh</a>
...
This works perfectly, the image is generated and sent back to the user whenever the captcha.ashx page is requested. My problem is that the HTTPHandler doesn't save the session?
I tried to get the session back from the normal page but i only got an exception saying that it didn't exist, so i turned on Trace to see what sessions that is active and it doesn't list the session that the HTTPHandler created (Captcha).
The HTTPHandler uses IReadOnlySessionState to interact with the sessions. Does the HTTPHandler only have read-access and thus not storing the session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试从命名空间实现 IRequiresSessionState 接口。
检查此链接:http: //anuraj.wordpress.com/2009/09/15/how-to-use-session-objects-in-an-httphandler/
Try Implementing IRequiresSessionState interface from namespace.
Check this link : http://anuraj.wordpress.com/2009/09/15/how-to-use-session-objects-in-an-httphandler/
您的处理程序需要实现 IRequiresSessionState。
Your Handler needs to implement IRequiresSessionState.