求ASP.NET中读取ASP的session的方法

shadow22 2005-06-16 09:00:13
由于以前系统一直用的ASP开发的代码,现在想转向ASP.NET,而以前的代码不可能都重写,造成了ASP和ASP.NET共存的现象.
我发现ASP.NET要读取ASP中的session是件很麻烦的事情,各位大哥有没有什么好的或者通用的解决方法,谢谢了.
此外,我还想问一下,系统中同时存在ASP和ASP.NET两种代码还有没有别的什么问题,请各位高手指点一下.
说明一下,小弟是初学者,以前是做嵌入式开发的,转行做这个才1个月时间,大家多多关照哦,谢谢.
...全文
210 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
goody9807 2005-06-16
  • 打赏
  • 举报
回复
ASP.NET实现

  在ASP.NET中每一个Web页都衍生自System.Web.UI.Page类。Page类集合了HttpSession对象的一个实例用于处理对话数据。在本例中,叫做SessionPage的自定义Page类来衍生自System.Web.UI.Page,提供了类似Page类的所有特性。唯一的区别是默认的HttpSession使用自定义的对话对象重载了(对实例变量使用new修改符,C#允许衍生的类隐藏基类的成员)。


public class SessionPage : System.Web.UI.Page
{
 ...
 public new mySession Session = null;
 ...
}

  自定义的对话类使用HybridDictionary对象来相应保存内存中的对话状态(HybridDictionary可用于处理任意数量的对话元素)。为了与传统的ASP通用,该自定义对话对象将限制对话数据类型为字符串型(默认的HttpSession允许对话保存任意类型的数据,不能与传统的ASP通用)。


[Serializable]
public class mySession
{
 private HybridDictionary dic = new HybridDictionary();

 public mySession()
 {
 }

 public string this [string name]
 {
  get
  {
   return (string)dic[name.ToLower()];
  }
  set
  {
   dic[name.ToLower()] = value;
  }
 }
}


  Page类为定制暴露了不同的事件和方法。特别是OnInit方法用于设置Page对象的初始化状态。如果请求不包含名为mySession的Cookie,将为请求者产生一个新的mySession Cookie。另外,对话数据将使用自定义数据访问对象SessionPersistence从SQL Server中检索出来。DSN和SessionExpiration的值从web.config中检索。


override protected void OnInit(EventArgs e)
{
 InitializeComponent();
 base.OnInit(e);
}
private void InitializeComponent()
{
 cookie = this.Request.Cookies[sessionPersistence.SessionID];

 if (cookie == null)
 {
  Session = new mySession();
  CreateNewSessionCookie();
  IsNewSession = true;
 }
 else
  Session = sessionPersistence.LoadSession(
    Server.UrlDecode(cookie.Value).ToLower().Trim(),
    dsn,
    SessionExpiration
   );

  this.Unload += new EventHandler(this.PersistSession);
}
private void CreateNewSessionCookie()
{
 cookie = new HttpCookie(sessionPersistence.SessionID,
 sessionPersistence.GenerateKey());
 this.Response.Cookies.Add(cookie);
}


  SessionPersistence类使用微软.NET框架组件的BinaryFormatter来串行化和并行化对话状态为二进制格式以提供最佳性能。结果的二进制对话数据接着作为图象字段类型被存入SQL Server。


public mySession LoadSession(string key, string dsn,
int SessionExpiration)
{
 SqlConnection conn = new SqlConnection(dsn);
 SqlCommand LoadCmd = new SqlCommand();
 LoadCmd.CommandText = command;
 LoadCmd.Connection = conn;
 SqlDataReader reader = null;
 mySession Session = null;

 try
 {
  LoadCmd.Parameters.Add("@ID", new Guid(key));
  conn.Open();
  reader = LoadCmd.ExecuteReader();
  if (reader.Read())
  {
   DateTime LastAccessed =reader.GetDateTime(1).AddMinutes(SessionExpiration);
   if (LastAccessed >= DateTime.Now)
    Session = Deserialize((Byte[])reader["Data"]);
  }
 }
 finally
 {
  if (reader != null)
   reader.Close();
  if (conn != null)
   conn.Close();
 }

 return Session;
}
private mySession Deserialize(Byte[] state)
{
 if (state == null) return null;

 mySession Session = null;
 Stream stream = null;

 try
 {
  stream = new MemoryStream();
  stream.Write(state, 0, state.Length);
  stream.Position = 0;
  IFormatter formatter = new BinaryFormatter();
  Session = (mySession)formatter.Deserialize(stream);
 }
 finally
 {
  if (stream != null)
   stream.Close();
 }
 return Session;
}


  在请求的末尾,Page类的Unload事件被启动了,一个同Unload事件一起注册的事件处理方法将串行化对话数据为二进制格式并将结果二进制数据存入SQL Server。

eyych 2005-06-16
  • 打赏
  • 举报
回复
up
shadow22 2005-06-16
  • 打赏
  • 举报
回复
搞定了,再次感谢各位的帮忙,结贴了.
tigerwen01 2005-06-16
  • 打赏
  • 举报
回复
asp和asp.net虽然能够一起工作,但他们是各干各的互不影响,所以他们的SESSION是不能共享的。
brando_beat 2005-06-16
  • 打赏
  • 举报
回复
好像如楼上所说。学习
shadow22 2005-06-16
  • 打赏
  • 举报
回复
谢谢楼上各位大哥,我先去看看你们提供的资料,十分感谢.
xzq686 2005-06-16
  • 打赏
  • 举报
回复
ASP和asp.net中SESSION不能共享.你也可以试试COOKIE.好像ASP中写的COOKIE和ASPNET中写的不一样.你可以查些资料
我碰到时是用第一种传参的方法了.就是从asp到aspx或 aspx到asp都经过处理传参

你可以试试这个用隐藏表单的方式来将asp.net中的session传入asp中:
http://www.eggheadcafe.com/articles/20021207.asp

另外还有一些其它方法:
http://www.eggheadcafe.com/articles/20041017.asp

另外.MSDN上有一种方法 :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/converttoaspnet.asp

hackate 2005-06-16
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/MSDN/library/WebServices/ASP.NET/HowtoShareSessionStateBetweenClassicASPandASP.NET.mspx

参考
luckyprg 2005-06-16
  • 打赏
  • 举报
回复
如果在两个站点中可以在ASP中POST到ASP.NET中再写一次Session。如果在同一个站点就不用了。
可以同时存在ASP和ASP.NET的。

62,052

社区成员

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

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

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

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