ASP到ASP.NET会话变量

前端之家收集整理的这篇文章主要介绍了ASP到ASP.NET会话变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们有一个经典asp的网站,我们正在慢慢迁移到ASP.NET.

问题当然是经典的asp和ASP.NET如何处理Sessions.在花了最后几个小时研究网络后,我发现了很多文章,但没有一篇文章比其他文章更突出.

是否有最佳实践将会话变量传递给经典的asp和asp.net?安全是必须的,并且非常感谢任何带有示例的解释.

解决方法

将经典asp中的单个会话变量传递给.net服务器端(从客户端隐藏会话值)的简单桥接器将是:

>在ASP端:输出会话的asp页面,例如调用它asp2netbridge.asp

  1. <%
  2. 'Make sure it can be only called from local server '
  3. if (request.servervariables("LOCAL_ADDR") = request.servervariables("REMOTE_ADDR")) then
  4. if (Request.QueryString("sessVar") <> "") then
  5. response.write Session(Request.QueryString("sessVar"))
  6. end if
  7. end if
  8. %>

>在.net端,远程调用该asp页面. :

  1. private static string GetAspSession(string sessionValue)
  2. {
  3. HttpWebRequest _myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://yourdomain.com/asp2netbridge.asp?sessVar=" + sessionValue));
  4. _myRequest.ContentType = "text/html";
  5. _myRequest.Credentials = CredentialCache.DefaultCredentials;
  6. if (_myRequest.CookieContainer == null)
  7. _myRequest.CookieContainer = new CookieContainer();
  8. foreach (string cookieKey in HttpContext.Current.Request.Cookies.Keys)
  9. {
  10. ' it is absolutely necessary to pass the ASPSESSIONID cookie or you will start a new session ! '
  11. if (cookieKey.StartsWith("ASPSESSIONID")) {
  12. HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieKey.ToString()];
  13. _myRequest.CookieContainer.Add(new Cookie(cookie.Name,cookie.Value,cookie.Path,string.IsNullOrEmpty(cookie.Domain)
  14. ? HttpContext.Current.Request.Url.Host
  15. : cookie.Domain));
  16. }
  17. }
  18. try
  19. {
  20. HttpWebResponse _myWebResponse = (HttpWebResponse)_myRequest.GetResponse();
  21.  
  22. StreamReader sr = new StreamReader(_myWebResponse.GetResponseStream());
  23. return sr.ReadToEnd();
  24. }
  25. catch (WebException we)
  26. {
  27. return we.Message;
  28. }
  29. }

猜你在找的asp.Net相关文章