ASP到ASP.NET会话变量

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

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

是否有最佳实践将会话变量传递给经典的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://www.f2er.com/aspnet/251040.html

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