Asp.net

ASP 到 ASP.NET 會話變數

  • July 20, 2017

我們有一個經典 ASP 網站,我們正在根據需要慢慢遷移到 ASP.NET。

問題當然是經典的 asp 和 ASP.NET 如何處理 Session。在花了最後幾個小時研究網路之後,我發現了很多文章,但沒有一篇比其他文章更突出。

是否有將會話變數從經典 asp 和 asp.net 傳輸到經典 asp 和 asp.net 的最佳實踐?安全性是必須的,非常感謝任何帶有範例的解釋。

將單個會話變數從經典 asp 傳遞到 .net 伺服器端(向客戶端隱藏會話值)的簡單橋接器是這樣的:

  • 在 ASP 端:一個用於輸出會話的 asp 頁面,稱之為 asp2netbridge.asp
<%
'Make sure it can be only called from local server '
if (request.servervariables("LOCAL_ADDR") = request.servervariables("REMOTE_ADDR")) then
   if (Request.QueryString("sessVar") <> "") then
       response.write Session(Request.QueryString("sessVar"))
   end if
end if
%>
  • 在.net 端,遠端呼叫該asp 頁面。:
private static string GetAspSession(string sessionValue)
{
   HttpWebRequest _myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://yourdomain.com/asp2netbridge.asp?sessVar=" + sessionValue));
   _myRequest.ContentType = "text/html";
   _myRequest.Credentials = CredentialCache.DefaultCredentials;
   if (_myRequest.CookieContainer == null)
       _myRequest.CookieContainer = new CookieContainer();
   foreach (string cookieKey in HttpContext.Current.Request.Cookies.Keys)
   {
       ' it is absolutely necessary to pass the ASPSESSIONID cookie or you will start a new session ! '
       if (cookieKey.StartsWith("ASPSESSIONID")) {
           HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieKey.ToString()];
           _myRequest.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrEmpty(cookie.Domain)
               ? HttpContext.Current.Request.Url.Host
               : cookie.Domain));
       }
   }
   try
   {
       HttpWebResponse _myWebResponse = (HttpWebResponse)_myRequest.GetResponse();

       StreamReader sr = new StreamReader(_myWebResponse.GetResponseStream());
       return sr.ReadToEnd();
   }
   catch (WebException we)
   {
       return we.Message;
   }
}

引用自:https://stackoverflow.com/questions/11251137