/// <summary> /// 检查当前校园网登录状态,并返回IP地址和地理位置 /// </summary> /// <returns>如果未登录,User的ID为0</returns> public async Task<SEUUser> CampusNetIsLogin() { Uri uri = new Uri("https://w.seu.edu.cn/portal/init.php"); string resp = await SEUHttpClientGet(uri); var definition = new { login = "" }; SEUUser seuUser = new SEUUser(); try { var jsonLogInfo = JsonConvert.DeserializeAnonymousType(resp, definition); if (jsonLogInfo.login != null) { var definition2 = new { login_username = "", login_ip = "", login_location = "" }; var jsonLogInfo2 = JsonConvert.DeserializeAnonymousType(resp, definition2); seuUser.StuID = jsonLogInfo2.login_username; seuUser.IpAddr = jsonLogInfo2.login_ip; seuUser.Location = jsonLogInfo2.login_location; } else { var definition2 = new { login_ip = "", login_location = "" }; var jsonLogInfo2 = JsonConvert.DeserializeAnonymousType(resp, definition2); seuUser.StuID = "0"; seuUser.IpAddr = jsonLogInfo2.login_ip; seuUser.Location = jsonLogInfo2.login_location; } } catch { } return seuUser; }
private async Task<SEUAuthJson> SEUFooLogin(SEUUser seuUser) { var netStatus = await myClient.CampusNetIsLogin(); if(netStatus.StuID != "0" ) { string baloonText = "当前已登录\r\n一卡通:" + netStatus.StuID + ",IP:" + netStatus.IpAddr +",地点:" + netStatus.Location; notifyIcon1.ShowBalloonTip(BaloonDelayTime, "无需重复连接", baloonText, System.Windows.Forms.ToolTipIcon.Info); labelIP.Text = "IP:" + netStatus.IpAddr; labelAddr.Text = "地点:" + netStatus.Location; labelSuccess.Text = "当前已登录"; return new SEUAuthJson(); } else { var loginResponse = await myClient.CampusNetLogin(seuUser); if(loginResponse.error == null) { string baloonText = "一卡通:" + loginResponse.login_username + ",IP:" + loginResponse.login_ip + ",地点:" + loginResponse.login_location; notifyIcon1.ShowBalloonTip(BaloonDelayTime, "连接成功", baloonText, System.Windows.Forms.ToolTipIcon.Info); labelIP.Text = "IP:" + netStatus.IpAddr; labelAddr.Text = "地点:" + netStatus.Location; labelSuccess.Text = "登录成功"; notifyIcon1.Icon = Properties.Resources.IconConnected; } else { labelSuccess.Text = "登录失败!"; labelIP.Text = "请检查网络连接,核对用户名和密码。"; labelAddr.Text = ""; notifyIcon1.ShowBalloonTip(BaloonDelayTime, "登录失败!", "请检查网络连接,核对用户名和密码。", System.Windows.Forms.ToolTipIcon.Error); } return loginResponse; } }
/// <summary> /// 使用一卡通号和密码进行登录 /// </summary> /// <param name="aUser"></param> /// <returns></returns> public async Task<SEUAuthJson> CampusNetLogin(SEUUser aUser) { var postdata = new FormUrlEncodedContent( new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("username",aUser.StuID), new KeyValuePair<string, string>("password",aUser.Pwd) }); Uri uri = new Uri("https://w.seu.edu.cn/portal/login.php"); string resp = await SEUHttpClientPost(uri, postdata); SEUAuthJson myResponseJson = JsonConvert.DeserializeObject<SEUAuthJson>(resp); return myResponseJson; }
private async void Connect_Click(object sender, EventArgs e) { seuUser = new SEUUser(textBoxStuID.Text, textBoxPwd.Text); await SEUFooLogin(seuUser); }
private async void buttonLogin_Click(object sender, EventArgs e) { bool bSavePwd = checkBoxSavePwd.Checked; bool bAutoLogin = checkBoxAutoLogin.Checked; seuUser = new SEUUser(textBoxStuID.Text, textBoxPwd.Text); SEUConfig newConfig = new SEUConfig(); newConfig.StuID = seuUser.StuID; newConfig.Pwd = seuUser.Pwd; newConfig.bSavePwd = bSavePwd; newConfig.bAutoLog = bAutoLogin; var netStatus = await SEUFooLogin(seuUser); if(netStatus.error == null) { newConfig.SaveToJson();//登录成功,帐号密码,配置参数保存到磁盘 } }
public SEUUser(SEUUser aSeuUser) { this.StuID = aSeuUser.StuID; this.Pwd = aSeuUser.Pwd; }