ashx中session问题

暗尘掩月 2012-05-22 12:49:17
通过ashx生成验证码 存入session中
在另外一个ashx中验证验证码是否正确 获取session为null


如果是通过aspx生成验证码 获取session就没有问题
...全文
141 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bonnibell 2012-05-22
  • 打赏
  • 举报
回复
public class Handler : IHttpHandler, IRequiresSessionState
孟子E章 2012-05-22
  • 打赏
  • 举报
回复
ashx文件要使用Session,必须实现Session接口;
using System;
using System.Web;
using System.Web.SessionState; //第一步:导入此命名空间

public class HttpHandler1 : IHttpHandler ,IRequiresSessionState //第二步:实现接口 到此就可以像平时一样用Session了
{
happytonice 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

ashx文件要使用Session,必须实现Session接口;
using System;
using System.Web;
using System.Web.SessionState; //第一步:导入此命名空间

public class HttpHandler1 : IHttpHandler ,IRequiresSessionState //第二步:实现接口 到此就可以像平时一……
[/Quote]
+1 正解
孟子E章 2012-05-22
  • 打赏
  • 举报
回复
先输出流,后面的代码都按流输出了,所以不行
调整顺序了
wtnu200 2012-05-22
  • 打赏
  • 举报
回复
StreamWriter sw = new StreamWriter(filename,false,Encoding.GetEncoding("gb2312"));
flyhorse999 2012-05-22
  • 打赏
  • 举报
回复
建议关注一下SESSIONID,看看,几次操作的SESSION,是不是同一个ID
暗尘掩月 2012-05-22
  • 打赏
  • 举报
回复
问题应该在这里
try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}

我把这个换成

MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.ClearContent();
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}

就可以了 没弄清楚究竟怎么回事
手可摘星辰 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
ashx文件要使用Session,必须实现Session接口;
using System;
using System.Web;
using System.Web.SessionState; //第一步:导入此命名空间

public class HttpHandler1 : IHttpHandler ,IRequiresSessionState //第二步:实现接口 到此就可以像平时一样……
[/Quote]


学习了
暗尘掩月 2012-05-22
  • 打赏
  • 举报
回复
不是接口的问题


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Web.SessionState;

namespace XKT.WebUI.ashx
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class validateimage : IHttpHandler, IRequiresSessionState,
{

/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext context)
{
//设置输出流图片格式
context.Response.ContentType = "image/gif";

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", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', '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)];
}
Bitmap bmp = new Bitmap(100, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int x2 = rnd.Next(bmp.Width);
int y2 = rnd.Next(bmp.Height);
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, 24);
Color clr = Color.Black;
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);

try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}


}

/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}


}
}

暗尘掩月 2012-05-22
  • 打赏
  • 举报
回复
接口都实现了 还是不行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Web.SessionState;

namespace XKT.WebUI.ashx
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class validateimage : IHttpHandler, IRequiresSessionState,
{

/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext context)
{
//设置输出流图片格式
context.Response.ContentType = "image/gif";

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", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', '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)];
}
Bitmap bmp = new Bitmap(100, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int x2 = rnd.Next(bmp.Width);
int y2 = rnd.Next(bmp.Height);
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, 24);
Color clr = Color.Black;
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);

try
{
bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
context.Response.End();
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}


}

/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}


}
}




public class login : IHttpHandler, IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string username = context.Request["uname"];
string password = context.Request["upass"];
string checkcode = context.Request["code"];

if (checkcode!=context.Session["xktinfo"].ToString().ToLower())
{
context.Response.Write("checkcodeerror");
}
else
{
if (username == "admin")
{
context.Response.Write("ok");
}
else
{
context.Response.Write("no");
}
}


}

public bool IsReusable
{
get
{
return false;
}
}
}

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧