• 把ASP应用中的Session传递给asp.net应用


    ASP.NET APPLICATION要使用原来的ASP用户系统,于是问题出现了,ASP APPLICATION怎样才能让用户登录的状态及用户信息在ASP.NET中依然有效呢。于是我们考虑用构造FORM来自动提交传递ASP应用中的Session变量。
    例子如下
    ASP应用URL为http://127.0.0.1/asp/,并在ASP.NET应用中的web.config设定
      <!--设定ASP应用的URL-->
      <add key="aspURL" value="http://127.0.0.1/asp/" />

    在ASP应用中增加两个ASP页面system.asp和autoPostForm.asp
    <!--system.asp-->
    <%
    Session("UID")="user"    
    session("isPass")="ok"    
    Server.Transfer("autoPostForm.asp")
    %>

    <!--autoPostForm.asp-->
    <%
    Response.Write("<form name=t id=t action=""http://127.0.0.1/aspdotnet/getSession.aspx""

    method=post >")
    Response.Write("<input type=hidden name=UID" )
    Response.Write( " value=" & Session("UID") & " >")
    Response.Write("<input type=hidden name=isPass" )
    Response.Write( " value=" & Session("isPass") & " >")
    Response.Write("</form>")
    Response.Write("<script>t.submit();</script>")
    %>

    在ASP.net应用中用页面getSession.aspx来接受传递过来的Session变量值

    getSession.aspx.cs代码片段:
    private void Page_Load(object sender, System.EventArgs e)
      {
      
      if(!Page.IsPostBack)
      {
       string aspurl=ConfigurationSettings.AppSettings["aspURL"].Trim();      
       try
       {
        string fromurl=Request.ServerVariables["HTTP_REFERER"];
        
        //验证是否从asp应用中提交过来
        if(fromurl.StartsWith(aspurl))
        {
         string uid=Request["UID"].ToString();
         string state=Request["isPass"].ToString();
        
          if(uid!="" && state=="ok")
          {
           //表明用户在asp系统中已登录成功 
        
          }
     
        }
        else
        {
         Response.Write("<script>alert('非法用户或未登录用户');top.location.href='" + aspurl +

    "';</script>");
        
        }
        
       }
       catch
       {
        Response.Redirect(aspurl);
       }

      }
    }

    当然,上述例子只是为解决特定的问题,如果要写成通用的,则需要做如下修改
    就在autoPostForm.asp使用 

    For each sItem in Session.Contents 
    Response.Write("<input type=hidden name=" & sItem) 
    Response.Write( " value=" & Session.Contents(sitem) & " >") 
    next 

    而在getSession.aspx页面用下面的代码来接受并用同名Session变量保存 

    for(int i=0;i<Request.Form.Count;i++) 

    Session[Request.Form.GetKey(i)]=Request.Form[i].ToString(); 

  • 相关阅读:
    基于正方系统的抢课软件教程系列一模拟登录1
    基于正方系统的抢课软件教程系列二正则表达式使用
    基于正方系统的抢课软件教程系列一模拟登录3之验证码识别
    eCos中断模型
    eCos下针对MIPS指令集的backtrace
    开源许可证GPL
    获取ListView中的ImageButton
    Rancher v2.4.8 容器管理平台集群搭建(基于k8s)
    Linux awk 替换文本字符串内容
    go: go.mod file not found in current directory or any parent directory; see 'go help mod 解决
  • 原文地址:https://www.cnblogs.com/top5/p/1794086.html
Copyright © 2020-2023  润新知