asp.net 在ashx文件中读写session

asp.net 在ashx文件中读写session

终于搞定在ashx中输出验证码,在保存验证码时竟然读不到session
错误提示:未将对象引用设置到对象的实例。 
查找资料终于搞定....

首先要继承 System.Web.SessionState.IReadOnlySessionState 的接口
其中:System.Web.SessionState.IReadOnlySessionState为只读会话的接口

而:System.Web.SessionState.IRequiresSessionState 为可读可写会话的接口,这个看情况而继承吧~~~

View Code
1 using System;
2
3  using System.Collections.Generic;
4
5 using System.Linq;
6
7 using System.Web;
8
9 using System.Drawing;
10
11 using System.Web.UI.WebControls;
12
13 using System.Web.SessionState;
14
15
16
17 namespace CanYou.FinancePlat.Web
18
19 {
20
21 /// <summary>
22
23 /// GeneraLoginCode 的摘要说明
24
25 /// </summary>
26
27 public class GeneraLoginCode : IHttpHandler, IRequiresSessionState
28
29 {
30
31 public void ProcessRequest(HttpContext context)
32
33 {
34
35 string checkCode = CreateCode(4);
36
37 context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
38
39 context.Session["_Login_ValidateCode"] = checkCode;
40
41 CreateImage(checkCode, context);
42
43 }
44
45 private string CreateCode(int codeLength)
46
47 {
48
49 string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
50
51 string[] strArr = so.Split(',');
52
53 string code = "";
54
55 Random rand = new Random();
56
57 for (int i = 0; i < codeLength; i++)
58
59 {
60
61 code += strArr[rand.Next(0, strArr.Length)];
62
63 }
64
65 return code;
66
67 }
68
69
70
71 private void CreateImage(string code, HttpContext context)
72
73 {
74
75 Bitmap image = new Bitmap(60, 20);
76
77 Graphics g = Graphics.FromImage(image);
78
79 WebColorConverter ww = new WebColorConverter();
80
81 g.Clear((Color)ww.ConvertFromString("#FAE264"));
82
83 Random random = new Random();
84
85 //画图片的背景噪音线
86
87 for (int i = 0; i < 12; i++)
88
89 {
90
91 int x1 = random.Next(image.Width);
92
93 int x2 = random.Next(image.Width);
94
95 int y1 = random.Next(image.Height);
96
97 int y2 = random.Next(image.Height);
98
99 g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
100
101 }
102
103 Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
104
105 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
106
107 new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true);
108
109 g.DrawString(code, font, brush, 0, 0);
110
111 //画图片的前景噪音点
112
113 for (int i = 0; i < 10; i++)
114
115 {
116
117 int x = random.Next(image.Width);
118
119 int y = random.Next(image.Height);
120
121 image.SetPixel(x, y, Color.White);
122
123 }
124
125 //画图片的边框线
126
127 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
128
129 System.IO.MemoryStream ms = new System.IO.MemoryStream();
130
131 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
132
133 context.Response.ClearContent();
134
135 context.Response.ContentType = "image/Gif";
136
137 context.Response.BinaryWrite(ms.ToArray());
138
139 g.Dispose();
140
141 image.Dispose();
142
143 }
144
145
146
147 public bool IsReusable
148
149 {
150
151 get
152
153 {
154
155 return false;
156
157 }
158
159 }
160
161 }
162
163 }

原文地址:https://www.cnblogs.com/xyfy/p/2015853.html