// Thread Code - Session 유지를 위해 실행될 코드 private void KeepSession() { // 무한 반복 while (true) { // CNSA net에서 저장된 Session 받아오기 string nowSession = Encoding.UTF8.GetString(Web.DownloadData(cnsaUrl + "/login/dupLoginCheck?loginId=" + cnsaId)); // Session이 저장된 세션과 다르고 저장된 세션이 비어있지 않을 경우 -> 저장된 Session 교체 if (!session.Equals(nowSession) && !nowSession.Trim().Equals("")) { // 저장된 Session 교체 session = nowSession; // CookieAwareWebClient의 Session 초기화 -> CookieContainer 초기화 Web.CookieContainer = new CookieContainer(); // Session 저장 Web.AddCookie(new Uri(urlIntraNet), new Cookie("JSESSIONID", session)); Web.AddCookie(new Uri(urlExtraNet), new Cookie("JSESSIONID", session)); } // 로그인을 통한 Session 갱신 Web.UploadValues(cnsaUrl + "/login/userLogin", new NameValueCollection() { { "loginId", cnsaId }, { "loginPw", cnsaPw } }); // 10초에 한 번 씩 실행 Thread.Sleep(10000); } }
// Session 유지를 위해 실행될 타이머 코드 void SessionUpdate(object state) { try { // CNSA net에서 저장된 Session 받아오기 string nowSession = Encoding.UTF8.GetString(Web.DownloadData(extraNetUrl + "/login/dupLoginCheck?loginId=" + cnsaId)); // Session이 저장된 세션과 다르고 저장된 세션이 비어있지 않을 경우 -> 저장된 Session 교체 if (!session.Equals(nowSession) && nowSession.Trim() != "") { // 저장된 Session 교체 session = nowSession; // Session이 변경되었을 경우 알림 new Handler(Looper.MainLooper).Post(new Java.Lang.Runnable(() => { Toast.MakeText(this, "Session이 변경되었습니다.", ToastLength.Long).Show(); })); } // Session 초기화 Web.CookieContainer = new CookieContainer(); // Session 저장 Web.AddCookie(new Uri(intraNetUrl), new Cookie("JSESSIONID", session)); Web.AddCookie(new Uri(extraNetUrl), new Cookie("JSESSIONID", session)); // 로그인을 통한 Session 갱신 Web.UploadValues(extraNetUrl + "/login/userLogin", new System.Collections.Specialized.NameValueCollection() { { "loginId", cnsaId }, { "loginPw", cnsaPw } }); } catch (WebException webE) { // WebException 발생 시 실행 [CNSAnet 연결 실패 포함] // Toast new Handler(Looper.MainLooper).Post(new Java.Lang.Runnable(() => { Toast.MakeText(this, "Session이 변경되었습니다.", ToastLength.Long).Show(); })); } }
// Form - 시작 버튼 클릭 시 private void startButton_Clicked(object sender, EventArgs e) { if (inputId.Text.Trim().Equals("")) // 아이디 입력란이 비어있을 경우 // inputId 초기화 후 포커스 맞춤 { inputId.Text = ""; inputId.Focus(); // MessageBox 띄움 MessageBox.Show("아이디를 입력해 주세요.", "로그인 실패", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (inputPw.Text.Trim().Equals("")) // 패스워드 입력란이 비어있을 경우 // inputPw 초기화 후 포커스 맞춤 { inputPw.Text = ""; inputPw.Focus(); // MessageBox 띄움 MessageBox.Show("패스워드를 입력해 주세요.", "로그인 실패", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { // 네트워크가 연결되어 있는지 확인 (인터넷 연결 여부는 관계없음) if (NetworkInterface.GetIsNetworkAvailable()) { // CookieAwareWebClient 생성 Web = new CookieAwareWebClient(); // CNSA net Id, Pw 저장 cnsaId = inputId.Text; cnsaPw = inputPw.Text; if (studentRadioButton.Checked) // 학생일 경우 { urlIntraNet = "http://10.1.100.32"; urlExtraNet = "https://student.cnsa.hs.kr"; } else // 선생님 || 교직원 일 경우 { urlIntraNet = "http://10.1.100.31"; urlExtraNet = "https://school.cnsa.hs.kr"; } // 사용할 큰사넷 주소 초기화 cnsaUrl = urlExtraNet; // 내부망 / 외부망 확인 스레드 생성 checkIntraNet = new Thread(CheckIntraNet); // 내부망 / 외부망 확인 시작 checkIntraNet.Start(); // CNSA net에서 Session을 받아옴 session = Encoding.UTF8.GetString(Web.DownloadData(cnsaUrl + "/login/dupLoginCheck?loginId=" + cnsaId)); // session이 존재할 경우 (로그인이 되어 있을 경우) if (!session.Trim().Equals("")) { // session 값을 저장 Web.AddCookie(new Uri(urlIntraNet), new Cookie("JSESSIONID", session)); Web.AddCookie(new Uri(urlExtraNet), new Cookie("JSESSIONID", session)); // 로그인 시도 string responseData = Encoding.UTF8.GetString(Web.UploadValues(cnsaUrl + "/login/userLogin", new NameValueCollection() { { "loginId", cnsaId }, { "loginPw", cnsaPw } })); // requestData에 특정 문자열이 포함되어 있을 경우 로그인 성공으로 간주 if (!responseData.Contains("/login/userLogin")) { // 현재 창 숨기기 this.Hide(); // Tray Icon 표시 notifyIcon.Visible = true; // Session 유지 스레드 생성 keepSession = new Thread(KeepSession); // Session 유지 시작 keepSession.Start(); // 알림 띄우기 notifyIcon.BalloonTipTitle = "Login Keeper"; notifyIcon.BalloonTipText = "CNSA Login Keeper를 시작합니다."; notifyIcon.BalloonTipIcon = ToolTipIcon.Info; notifyIcon.ShowBalloonTip(3000); // Session 유지 시작 -> openToolStripMenuItem.Text = "사용자 변경" 으로 설정 reloginToolStripMenuItem.Text = "사용자 변경"; } else // 로그인 실패 // MessageBox 띄움 { MessageBox.Show("로그인 정보가 일치하지 않습니다.", "로그인 실패", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else // session이 존재하지 않을 경우 (로그인이 되어 있지 않을 경우) // MessageBox 띄움 { MessageBox.Show("유지할 데이터가 존재하지 않습니다.\n아이디와 패스워드 또는 현재 로그인 되어있는지를 확인해 주세요.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("네트워크 연결상태를 확인해 주세요.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
// Button Click Event private void CLK_Click(object sender, EventArgs e) { // CNSAnet Id, Pw 저장 cnsaId = FindViewById <EditText>(Resource.Id.inputId).Text; cnsaPw = FindViewById <EditText>(Resource.Id.inputPw).Text; if (IsStarted) // Service가 실행 중인 경우 // Service 종료 { StopService(CLKService); // Service 실행 상태 갱신 IsStarted = false; // Button Image 변경 FindViewById <Button>(Resource.Id.loginLogo).Background = GetDrawable(Resource.Drawable.CLKStop); } else if (cnsaId.Trim().Equals("")) // If ID column is empty // inputId 초기화 후 포커스 맞춤 { FindViewById <EditText>(Resource.Id.inputId).Text = ""; FindViewById <EditText>(Resource.Id.inputId).FocusableInTouchMode = true; FindViewById <EditText>(Resource.Id.inputId).RequestFocus(); // Toast Toast.MakeText(this, "Please enter your ID", ToastLength.Long).Show(); } else if (cnsaPw.Trim().Equals("")) // If PW column is empty // inputPw 초기화 후 포커스 맞춤 { FindViewById <EditText>(Resource.Id.inputPw).Text = ""; FindViewById <EditText>(Resource.Id.inputPw).FocusableInTouchMode = true; FindViewById <EditText>(Resource.Id.inputPw).RequestFocus(); // Toast Toast.MakeText(this, "Please enter your PW", ToastLength.Long).Show(); } else { try { // CNSAnet에서 Session을 받아옴 session = Encoding.UTF8.GetString(Web.DownloadData(extraNetUrl + "/login/dupLoginCheck?loginId=" + cnsaId)); } catch (WebException webE) { // WebException 발생 시 실행 [CNSAnet 연결 실패 포함] // Toast Toast.MakeText(this, "CNSAnet에 연결할 수 없습니다.", ToastLength.Long).Show(); return; } // Session이 존재할 경우 (로그인이 되어 있을 경우) if (session.Trim() != "") { // Session 초기화 Web.CookieContainer = new CookieContainer(); // Session 값을 저장 Web.AddCookie(new Uri(intraNetUrl), new Cookie("JSESSIONID", session)); Web.AddCookie(new Uri(extraNetUrl), new Cookie("JSESSIONID", session)); // 로그인 시도 string responseData = Encoding.UTF8.GetString(Web.UploadValues(extraNetUrl + "/login/userLogin", new System.Collections.Specialized.NameValueCollection() { { "loginId", cnsaId }, { "loginPw", cnsaPw } })); if (!responseData.Contains("/login/userLogin")) // requestData에 특정 문자열이 포함되어 있지 않은 경우 로그인 성공으로 간주 // Service가 실행 중이지 않은 경우 { if (!IsStarted) { // Create Service CLKService = new Intent(this, typeof(KeepSession)); // Set Service Action CLKService.SetAction("com.CLK.KeepService"); // Add Receiver receiver = new RestartService(); // Save Receiver RegisterReceiver(receiver, new IntentFilter(CLKService.Action)); // Service에 값 전달 CLKService.PutExtra("cnsaId", cnsaId); CLKService.PutExtra("cnsaPw", cnsaPw); if (studentCount) // 학생인 경우 { CLKService.PutExtra("intraNetUrl", strIntraNetUrl); CLKService.PutExtra("extraNetUrl", strExtraNetUrl); } else // 교사/교직원인 경우 { CLKService.PutExtra("intraNetUrl", tcrIntraNetUrl); CLKService.PutExtra("extraNetUrl", tcrExtraNetUrl); } CLKService.PutExtra("session", session); // Service 실행 StartService(CLKService); // Service 상태 저장 IsStarted = true; // Button Image 변경 FindViewById <Button>(Resource.Id.loginLogo).Background = GetDrawable(Resource.Drawable.CLKRun); } } else // 로그인 실패 // Toast { Toast.MakeText(this, "Login Failed", ToastLength.Long).Show(); } } else // session이 존재하지 않을 경우 (로그인이 되어 있지 않을 경우) // Toast { Toast.MakeText(this, "CNSAnet에 로그인이 되어있지 않습니다.", ToastLength.Long).Show(); } } }