亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > ASP > 正文

How to Share Session State Between Classic ASP and

2024-05-04 11:06:31
字體:
來源:轉載
供稿:網友
applies to:
   microsoft® asp.net

summary: discusses how to share session state between classic asp and microsoft asp.net using microsoft .net framework classes and the serialization feature of the .net framework. sharing session state allows converting existing asp applications to asp.net applications in stages while running the applications side by side. (12 printed pages)

download the source code for this article.

contents
introduction
conceptual overview
asp.net implementation
asp implementation
demo program
incorporating the com object in an existing asp application
limitation/improvement
conclusion

introduction
microsoft® asp.net is the latest microsoft technology for developing web-based applications. it offers a number of advantages over the classic asp script technology, including: 1) a better development structure by separating the ui presentation from business logic; 2) its code is fully compiled instead of interpreted as in classic asp; and 3) its compile feature in conjunction with its caching support means significantly better performance for sites written in asp.net over equivalent sites written in classic asp.

despite the potential benefit of converting existing asp applications to asp.net, many existing asp applications are mission critical and complex. the conversion process could be resource intensive and induce additional risk to the existing application. one approach to address these issues is to run the asp and asp.net side by side, and convert one section of the application at a time to asp.net. in order to run the new and old application side by side, a mechanism is needed to share the session state between classic asp and asp.net. in this article, i'll discuss how the session state can be shared by using several classes and the serialization feature of the microsoft® .net framework.

conceptual overview
cookies are the most common way for web applications to identify the user session, and can be used to identify session state for both classic asp and asp.net. session state information is stored in memory in asp script and can't be shared with other applications, such as asp.net. if the session state is stored in a common format in microsoft® sql server, the session state can be accessible by both classic asp and asp.net.

in this example, a cookie named mysession is used to identify the user session. when a user makes a request to the web application, the user will be issued a unique cookie to identify the session. on subsequent request, the browser will send the unique cookie back to the server to identify the session. before the requested web page is loaded, a custom object will reload the user session data from sql server using the unique cookie. the session state is accessible in the web page through the custom object. after the web request is finished, the session data will be persisted back to the sql server as the request terminates (see figure 1).



figure 1. sample data flow

asp.net implementation
in asp.net, every web page derives from the system.web.ui.page class. the page class aggregates an instance of the httpsession object for session data. in this example, a custom page class called sessionpage is derived from the system.web.ui.page to offer all the same features as the page class. the only difference with the derived page is that the default httpsession is overridden with a custom session object. (using the new modifier for the instance variable, c# allows the derived class to hide members of the base class.)

   public class sessionpage : system.web.ui.page
   {
      ...
      public new mysession session = null;
      ...
   }

the custom session class is responsible for storing the session state in memory using the hybriddictionary object. (hybriddictionary can efficiently handle any number of session elements.) the custom session class will limit the session data type to be string only for interoperability with the classic asp. (the default httpsession allows any type of data to be stored in the session, which will not interoperate with the classic 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;
         }
      }
   }

the page class exposes different events and methods for customization. in particular, the oninit method is used to set the initialize state of the page object. if the request does not have the mysession cookie, a new mysession cookie will be issued to the requester. otherwise, the session data will be retrieved from sql server using a custom data access object, sessionpersistence. the dsn and sessionexpiration values are retrieved from the 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);
      }

the sessionpersistence class uses the binaryformatter of the microsoft .net framework to serialize and deserialize the session state in binary format for optimal performance. the resulting binary session state data can then be stored in the sql server as an image field type.

      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;
      }

at the end of the request, the page class unload event is fired, and an event handler registered with the unload event will serialize the session data into binary format and save the resulting binary data into sql server.

      private void persistsession(object obj, system.eventargs arg)
      {      sessionpersistence.savesession(
               server.urldecode(cookie.value).tolower().trim(),
               dsn, session, isnewsession);
      }
      public void savesession(string key, string dsn,
mysession session, bool isnewsession)
      {
         sqlconnection conn = new sqlconnection(dsn);
         sqlcommand savecmd = new sqlcommand();         
         savecmd.connection = conn;
         
         try
         {
            if (isnewsession)
               savecmd.commandtext = insertstatement;
            else
               savecmd.commandtext = updatestatement;

            savecmd.parameters.add("@id", new guid(key));
            savecmd.parameters.add("@data", serialize(session));
            savecmd.parameters.add("@lastaccessed", datetime.now.tostring());
      
            conn.open();
            savecmd.executenonquery();
         }
         finally
         {
            if (conn != null)
               conn.close();
         }
      }
private byte[] serialize(mysession session)
      {
         if (session == null) return null;

         stream stream = null;
         byte[] state = null;

         try
         {
            iformatter formatter = new binaryformatter();
            stream = new memorystream();
            formatter.serialize(stream, session);
            state = new byte[stream.length];
            stream.position = 0;
            stream.read(state, 0, (int)stream.length);
            stream.close();
         }
         finally
         {
            if (stream != null)
               stream.close();
         }
         return state;
      }

the sessionpage class and its associated classes are packaged in the sessionutility assembly. in a new asp.net project, a reference will be made to the sessionutility assembly, and every page will derive from the sessionpage instead of from the page class in order to share session with classic asp codes. once the porting is completed, the new application can switch back to use the native httpsession object by commenting out the session variable declaration in the sessionpage class to unhide the base httpsession.

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美乱大交xxxxx另类电影| 色777狠狠综合秋免鲁丝| 美女av一区二区三区| 91美女片黄在线观| 一本大道久久加勒比香蕉| 97精品在线观看| 欧美激情极品视频| 亚洲精品小视频| 国产成人啪精品视频免费网| 欧美一区二区三区精品电影| 欧美午夜久久久| 精品视频在线观看日韩| 北条麻妃一区二区在线观看| 在线日韩日本国产亚洲| 欧美大片欧美激情性色a∨久久| 欧美一乱一性一交一视频| 亚洲精品视频中文字幕| 欧美日韩国产精品一区| 日韩在线视频网| 亚洲电影第1页| 欧美精品福利在线| 精品国产自在精品国产浪潮| 欧美视频专区一二在线观看| 欧美肥婆姓交大片| 日本国产欧美一区二区三区| 亚洲欧洲在线视频| 久久影院中文字幕| 日韩在线观看网址| 久久高清视频免费| 亚洲品质视频自拍网| 亚洲最大成人在线| 国产丝袜一区二区三区免费视频| 中文字幕亚洲自拍| 日韩精品视频在线观看网址| 成人免费视频在线观看超级碰| 国模视频一区二区| 欧美日韩免费在线观看| 精品国产精品三级精品av网址| 久久免费视频网| 欧美在线精品免播放器视频| 久久伊人91精品综合网站| 日韩精品视频免费专区在线播放| 性视频1819p久久| 中文字幕在线看视频国产欧美| 国产亚洲激情视频在线| 欧美极品少妇xxxxⅹ免费视频| 亚洲精品有码在线| 欧美性理论片在线观看片免费| 亚洲人成电影网站色www| 欧美性猛交99久久久久99按摩| 欧美最猛性xxxxx(亚洲精品)| 亚洲精品av在线播放| 国产97在线观看| 国产香蕉一区二区三区在线视频| 国产原创欧美精品| 精品无人区太爽高潮在线播放| 精品在线欧美视频| 欧美日韩国产精品一区二区不卡中文| 精品一区精品二区| 欧美性生交xxxxxdddd| 久久久久久久一| 国产成人精品电影久久久| 日韩成人激情影院| 亚洲第一中文字幕| 成人免费看黄网站| 精品视频久久久| 色综合久久精品亚洲国产| 18一19gay欧美视频网站| 国产精品久久久久久五月尺| 国产精品视频一| 国产精品2018| 国产一区二区三区免费视频| 久久精品视频亚洲| 精品少妇v888av| 超碰精品一区二区三区乱码| 精品视频在线播放免| 精品成人久久av| 久久精品电影一区二区| 国产精品成人国产乱一区| 久久久爽爽爽美女图片| 丝袜美腿亚洲一区二区| 97超级碰碰碰| 欧美高跟鞋交xxxxhd| 日韩中文字幕国产| 欧美成人激情视频| 亚洲成人av片在线观看| 亚洲自拍偷拍在线| 国产精品丝袜高跟| 欧美成人激情图片网| 97在线视频观看| 国内精品久久久| 日韩av在线最新| 亚洲精品一区二三区不卡| 精品国产乱码久久久久久天美| 自拍偷拍亚洲精品| 欧美日韩中国免费专区在线看| 日韩av在线免费观看一区| 人九九综合九九宗合| 国产亚洲人成网站在线观看| 影音先锋日韩有码| 在线视频免费一区二区| 国产精品福利网站| 久久久久久久电影一区| 夜色77av精品影院| 亚洲精品自拍偷拍| 欧美一级片在线播放| 粉嫩av一区二区三区免费野| 4k岛国日韩精品**专区| 国产欧美婷婷中文| 国产精品女视频| 亚洲欧洲国产伦综合| 日韩欧美中文字幕在线播放| 日韩毛片在线观看| 国产有码在线一区二区视频| 欧美极品欧美精品欧美视频| 亚洲aⅴ日韩av电影在线观看| 高清一区二区三区日本久| 亚洲字幕在线观看| 丝袜亚洲另类欧美重口| 91精品久久久久久久久中文字幕| 欧美精品久久久久久久久久| 最近中文字幕mv在线一区二区三区四区| 亚洲欧美国产高清va在线播| 久久久久久久久久婷婷| 久久久免费av| 欧美成aaa人片在线观看蜜臀| 国产一区二区三区视频免费| 海角国产乱辈乱精品视频| 久久99久久久久久久噜噜| 欧美大片免费观看在线观看网站推荐| 亚洲www在线观看| 国产精品美女主播| 97在线看免费观看视频在线观看| 91在线精品播放| 亚洲视频电影图片偷拍一区| 日本久久久久久久久| 97国产精品视频人人做人人爱| 91夜夜揉人人捏人人添红杏| 欧美理论电影在线观看| 欧美激情一区二区三区成人| 欧美激情视频给我| 国产在线观看不卡| 亚洲欧美制服中文字幕| 欧美成人免费全部观看天天性色| 国产精品91一区| 国自产精品手机在线观看视频| 日韩av手机在线观看| 国产精品一区二区性色av| 欧美高清视频在线播放| 国产一区二区三区直播精品电影| 韩国国内大量揄拍精品视频| 中文字幕日韩av| 亚洲欧洲一区二区三区在线观看| 国产精品久久久久免费a∨大胸| 欧美最猛黑人xxxx黑人猛叫黄| www.亚洲一二| 日韩精品中文字| 一本色道久久综合狠狠躁篇的优点| 免费不卡欧美自拍视频| 91性高湖久久久久久久久_久久99| 日韩免费看的电影电视剧大全| 国产成人精品国内自产拍免费看| 欧美黑人xxx| 色偷偷91综合久久噜噜|