项目经验之:项目中多个子系统之间共享登陆解决方案求解_也可叫免登陆

当,,,项目中现在有这样一种情况,,,,想要做到只需登陆一次即可,访问所有的外围子系统...........

也就是说子系统各有一套权限与登陆界面,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

现在我想要在不改动子系统的情况下,,,,,,,,,,,,,做到只需登陆一次即可访问所有的系统...................

我在网上看到一种方法,测试了下,能得到子系统的HTML源代码,,,,,,,但路径出现问题..............

不知大家有什么好的解决方案...............在些学习下

注意单点登陆是不能解决这说的这种情况的.................不清楚大家想过没有,

子系统的登陆存储是各不相同的..............

  1 private readonly string BBSURL = "http://222.35.90.15/4007/";
  2     private readonly string USERNAME = "zhouna";
  3     private readonly string PASSWORD = "sa";
  4 
  5     private CookieCollection gCookieCollention = null;
  6     private HttpWebRequest BBSRequest = null;
  7     private HttpWebResponse BBSResponse = null;
  8 
  9     protected void Button8_Click(object sender, EventArgs e)
 10     {
 11         string loginUrl = string.Format("{0}index.aspx", BBSURL);
 12         RemoveCookies();
 13         MaliciousLogin(loginUrl, USERNAME, PASSWORD);
 14         //Response.Redirect(loginUrl);
 15     }
 16 
 17 /// <summary>
 18     /// 自动登录
 19     /// </summary>
 20     public void MaliciousLogin(string loginUrl, string usr, string pwd)
 21     {
 22         string responseHTML = string.Empty; ;
 23         string loginstr = string.Format("__VIEWSTATE=dDwtMTk2Njk4MTQwMjs7PoBcGF95DrGh94QfuOwV438Bncs1&txt_user={0}&txt_passwd={1}&btn_Ok=%E7%A1%AE%E8%AE%A4GET", usr, pwd);//这里我是用截请流和工具得到子系统中的传入参数因为子系统我看不到源代码---------------------------------------
 24         loginstr = EncodePost(loginstr);
 25         byte[] replybyte = Encoding.UTF8.GetBytes(loginstr);
 26 
 27         try
 28         {
 29             CookieContainer _cookieContainer = new CookieContainer();
 30             BBSRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
 31             BBSRequest.CookieContainer = _cookieContainer;
 32 
 33             BBSRequest.ContentType = "application/x-www-form-urlencoded";
 34             BBSRequest.Method = "POST";
 35             BBSRequest.AllowAutoRedirect = true;
 36             //post 开始
 37             BBSRequest.ContentLength = replybyte.Length;
 38             Stream newStream = BBSRequest.GetRequestStream();
 39             newStream.Write(replybyte, 0, replybyte.Length);
 40             newStream.Close();
 41             
 42             //post 结束
 43 
 44             //返回HTML
 45             BBSResponse = (HttpWebResponse)BBSRequest.GetResponse();
 46             Stream dataStream = BBSResponse.GetResponseStream();
 47             StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("utf-8"));
 48             responseHTML = reader.ReadToEnd();
 49 
 50 
 51             
 52             gCookieCollention = BBSResponse.Cookies;
 53 
 54 
 55             string PhotoClassURL = "http://222.35.90.5/4007/CodeLevelList.aspx";
 56             HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
 57             foreach (Cookie temp in BBSResponse.Cookies)
 58             {
 59                 temp.Domain = "222.35.90.5";
 60             }
 61             _cookieContainer = BBSRequest.CookieContainer;
 62 
 63             Myrequest.CookieContainer = _cookieContainer;
 64 
 65            
 66             HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
 67             Myresponse.Cookies = _cookieContainer.GetCookies(Myresponse.ResponseUri);
 68             //_cookieContainer.Add(Myresponse.Cookies);
 69             Stream Mystream = Myresponse.GetResponseStream();
 70             string sHtml = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();
 71 
 72 
 73 Response.write(sHtml)//这里sHtml我就得到了子系统访问页的源代码.
 74 
 75         }
 76         catch (Exception ex)
 77         {
 78             //MessageBox.Show(ex.ToString());
 79         }
 80 
 81 
 82     }
 83     private string EncodePost(string input)
 84     {
 85         string output = null;
 86         Char[] reserved = { '?''=''&' };
 87         if (input != null)
 88         {
 89             int i = 0, j;
 90             while (i < input.Length)
 91             {
 92                 j = input.IndexOfAny(reserved, i);
 93                 if (j == -1)
 94                 {
 95                     output = output + HttpUtility.UrlEncode(input.Substring(i, input.Length - i), System.Text.Encoding.GetEncoding("utf-8"));
 96                     break;
 97                 }
 98                 string tt = HttpUtility.UrlEncode(input.Substring(i, j - i), System.Text.Encoding.GetEncoding("utf-8"));
 99                 output += tt;
100                 output += input.Substring(j, 1);
101                 i = j + 1;
102             }
103             return output;
104         }
105         else
106             return null;
107     }
108 
109     private void RemoveCookies()
110     {
111         int cookiesmax = Environment.GetFolderPath(Environment.SpecialFolder.Cookies).Length;
112         for (int i = 0; i < cookiesmax; i++)
113             Environment.GetFolderPath(Environment.SpecialFolder.Cookies).Remove(0);
114     }
青华木园
原文地址:https://www.cnblogs.com/accpfriend/p/1555879.html