C# 会话未从 HTTPHandler 保存

发布于 2024-08-07 19:47:00 字数 1276 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2024-08-14 19:47:00

尝试从命名空间实现 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/

财迷小姐 2024-08-14 19:47:00

您的处理程序需要实现 IRequiresSessionState。

public class CaptchaHandler : IHttpHandler, IRequiresSessionState
{
...
}

Your Handler needs to implement IRequiresSessionState.

public class CaptchaHandler : IHttpHandler, IRequiresSessionState
{
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文