62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Xml;
namespace ECInterSys
{
/// <summary>
/// testSession 的摘要说明
/// </summary>
public class testSession : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string login = context.Request["login"];
if (login!=null&&login.Equals("yes"))
{
string loginName = context.Request["loginName"];
//string pwd= context.Request["pwd"];
//.....验证用户密码正确性
string newToken = Guid.NewGuid().ToString();
context.Session[newToken] = loginName;
//writeFile(newToken, loginName);//这个是写入服务器外部文件的demo,若执行这一句话,则再次访问时context.Session[token]==null
context.Response.Write(newToken);
return;
}
string token = context.Request["token"];
if (!string.IsNullOrEmpty(token))
{
//验证令牌
if (context.Session!=null&& context.Session[token]!=null)
{
context.Response.Write(context.Session[token]);
return;
}
}
context.Response.Write("hello!");
}
//测试Session存储若写入本地文件则Session消息
//示例中写入本地文件的内容仅供参考,实际使用中不写登录信息
//示例仅仅是表示需要写外部文件
private void writeFile(string token,string loginName)
{
string fileName= string.Format(@"{0}bin\outerFile", AppDomain.CurrentDomain.BaseDirectory.ToString());
try
{
XmlDocument xml = new XmlDocument();
if (!File.Exists(fileName))
{
XmlDeclaration declaration = xml.CreateXmlDeclaration("1.0", "GB2312", "");//xml文档的声明部分
xml.AppendChild(declaration);
XmlElement xeRoot = xml.CreateElement("root");//创建一个<thumb>节点
xml.AppendChild(xeRoot);
}
else
xml.Load(fileName);
XmlNode xnRoot = xml.SelectSingleNode("//root");
XmlElement xeSub = xml.CreateElement("item");
xeSub.SetAttribute("ID", token);
xeSub.SetAttribute("loginName", loginName);
xml.Save(fileName);
}
catch(Exception ex)
{
throw ex;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}