ashx中session问题 通过ashx生成验证码 存入session中 在另外一个ashx中验证验证码是否正确 获取session为null如果是通过aspx生成验证码 获取session就没有问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本帖最后由 net_lover 于 2012-05-22 12:57:12 编辑 public class Handler : IHttpHandler, IRequiresSessionState 接口都实现了 还是不行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; } } } 不是接口的问题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; } } }} 问题应该在这里 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(); }就可以了 没弄清楚究竟怎么回事 建议关注一下SESSIONID,看看,几次操作的SESSION,是不是同一个ID StreamWriter sw = new StreamWriter(filename,false,Encoding.GetEncoding("gb2312")); 本帖最后由 net_lover 于 2012-05-22 13:26:38 编辑 请教个GridView的取值问题 回调参数问题! Repeater模板里控件问题 引用控件到页面.出现错误. 求助 请问关于dbgrid的itemcommand事件 关于把登陆用户实时在Datagrid中显示出来,如何实现?分数高,解决马上给分! 刚接触ASP。NET没多久,请大家一定指教一下,现在有一问题无法解决还请高人指点,在线等待,答者给分! 请问安装.net2005与现有的.net2003会不会冲突? 【求助】如何获取frameset中其他页的状态? SlideShowExtender 有关form验证的问题
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;
}
}
}
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;
}
}
}
}
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();
}就可以了 没弄清楚究竟怎么回事