public CookieInfo GetCookieInfoByCookieUserToken(string userToken)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.fileName);
                XmlNode root = doc.DocumentElement;

                //历史Cookies
                XmlNodeList historyCookieNodes = root.SelectNodes("ItemGroup/Cookie");
                foreach (XmlNode cookieNode in historyCookieNodes)
                {
                    string cookie = cookieNode.InnerText.Trim();
                    string tk     = CookieUtil.GetCookieValue(cookie, "_tk");
                    if (!tk.Equals(userToken, StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }

                    return(XmlNode2CookieInfo(cookieNode));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(null);
        }
Exemple #2
0
        private void HandleCookie(string cookieString)
        {
            if (cookieString == null)
            {
                return;
            }

            string userToken = CookieUtil.GetCookieValue(cookieString, "_tk", false);
            string webToken  = CookieUtil.GetCookieValue(cookieString, "_wtk", false);

            if (userToken != null)
            {
                AddCookie(userToken, webToken);
            }
        }
Exemple #3
0
        private void HandleCookie(CookieCollection cookieCollection)
        {
            if (cookieCollection == null)
            {
                return;
            }

            string userToken = CookieUtil.GetCookieValue(cookieCollection, "_tk", false);
            string webToken  = CookieUtil.GetCookieValue(cookieCollection, "_wtk", false);

            if (userToken != null)
            {
                AddCookie(userToken, webToken);
            }
        }
Exemple #4
0
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOk_Click(object sender, EventArgs e)
        {
            string cookie = base.GetControlText(this.textBoxCookie).Trim();

            if (cookie.Length < 1)
            {
                MessageBox.Show("请输入Cookie!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //登录
            this.CookieString = cookie;
            var    config = new ConfigStorage(GlobalContext.ConfigFileName);
            Thread thread = new Thread(() =>
            {
                try
                {
                    ChangeControlState(false);
                    this.MyUserInfo = GlobalContext.PH.GetUserInfo(cookie);

                    if (CertPath == null)
                    {
                        CookieInfo foundCookieInfo;
                        if ((foundCookieInfo = config.GetCookieInfoByUserId(this.MyUserInfo.id)) != null)
                        {
                            CertPath = foundCookieInfo.CertPath;
                        }
                    }

                    // 更新配置文件中的账户信息
                    config.UpdateCookieInfo(new CookieInfo()
                    {
                        Id       = this.MyUserInfo.id,
                        Name     = this.MyUserInfo.nick,
                        Cookie   = cookie,
                        CertPath = CertPath
                    });

                    DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    GlobalContext.MainForm.Output("登录失败:" + ex.Message);
                    this.CookieString = null;
                    this.CertPath     = null;
                    if (ex.Message.Contains("已过期"))
                    {
                        GlobalContext.Output("Cookie已过期,正在更新Cookie...");
                        string tk      = CookieUtil.GetCookieValue(cookie, "_tk");
                        var cookieInfo = config.GetCookieInfoByCookieUserToken(tk);
                        string id      = null;
                        if (cookieInfo == null || cookieInfo.Id == null)
                        {
                            var frm             = new FrmInputText();
                            frm.Text            = "请输入健康号";
                            frm.FormBorderStyle = FormBorderStyle.Sizable;
                            frm.StartPosition   = FormStartPosition.CenterScreen;
                            if (frm.ShowDialog() != DialogResult.OK)
                            {
                                ChangeControlState(true);
                                return;
                            }
                            id = frm.InputText.Trim();
                        }
                        else
                        {
                            id = cookieInfo.Id.ToString();
                        }

                        string certPath = null;
                        if (cookieInfo == null || !File.Exists(cookieInfo.CertPath))
                        {
                            var ofd         = new OpenFileDialog();
                            ofd.Title       = "请选择证书";
                            ofd.Filter      = "证书文件(*.pfx)|*.pfx";
                            ofd.FilterIndex = 1;
                            ofd.Multiselect = false;
                            if (ofd.ShowDialog() != DialogResult.OK)
                            {
                                ChangeControlState(true);
                                return;
                            }
                            certPath = ofd.FileName;
                        }
                        else
                        {
                            certPath = cookieInfo.CertPath;
                        }

                        try
                        {
                            var result = GlobalContext.PH.RenewUserTokenAndWebToken(id, cookie, certPath);
                            if (result.Stat.Code == 0)
                            {
                                string cookieNew = string.Empty;
                                cookieNew       += "_tk=" + System.Web.HttpUtility.UrlEncode(result.Content[0].Token) + ";"; // 必须进行urlencode
                                cookieNew       += "_wtk=" + result.Content[0].WebToken + ";";
                                SetControlText(this.textBoxCookie, cookieNew);

                                this.CertPath = certPath;

                                // 重新登录
                                buttonOk_Click(null, null);

                                //StringBuilder builder = new StringBuilder();
                                //builder.AppendLine("Uid:" + result.Content[0].Uid);
                                //builder.AppendLine("Tk:" + result.Content[0].Token);
                                //builder.AppendLine("Wtk:" + result.Content[0].WebToken);
                                //builder.AppendLine("Expire:" + result.Content[0].GetExpireTime());
                                //builder.AppendLine("LockTime:" + result.Content[0].LockTime);
                                //builder.AppendLine("请重新点击登录!");

                                //MsgBox.ShowInfo(builder.ToString(), "更新成功");
                            }
                        }
                        catch (Exception ex2)
                        {
                            MsgBox.ShowInfo("更新失败," + ex2.Message);
                        }
                        ChangeControlState(true);
                        return;
                    }
                    else if (ex.Message.Contains("设备上退出"))
                    {
                        config.RemoveCookieInfo(new CookieInfo {
                            Cookie = this.CookieString
                        });
                    }

                    MessageBox.Show("登录失败," + ex.Message);
                    ChangeControlState(true);
                }
                finally
                {
                    //重新读取历史Cookie信息
                    ConfigInfo configInfo        = config.GetConfigInfo();
                    GlobalContext.HistoryCookies = configInfo.HistoryCookies;
                }
            });

            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }