public UserInfoItem GetUserByEmail(string AccessToken, string email) { string user = email; try { HttpWebRequest request = WebRequest.Create("https://graph.microsoft.com/v1.0/users/" + user) as HttpWebRequest; var myHttpWebRequest = (HttpWebRequest)request; request.Method = "GET"; myHttpWebRequest.PreAuthenticate = true; myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken); myHttpWebRequest.Accept = "application/json"; var myWebResponse = request.GetResponse(); var responseStream = myWebResponse.GetResponseStream(); string objText = null; using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.UTF8)) { //while (myStreamReader.Peek()>=0) { objText = myStreamReader.ReadToEnd(); //Console.WriteLine(objText); //} } JavaScriptSerializer js = new JavaScriptSerializer(); var myojb = (UserInfoItem)js.Deserialize(objText, typeof(UserInfoItem)); UserInfoItem userobject = myojb; responseStream.Close(); myWebResponse.Close(); return(userobject); } catch (Exception e) { MessageBox.Show(e.Message); return(null); } }
private Task testFuctions(string fid, string c_id, string ttid, string tcid, int map_id) { return(Task.Run(async() => { string stacd = "2"; string token7 = GetToken(stacd); List <MapUser> ue = GetUsrMap(); List <MapMenUser> ftemail = new List <MapMenUser>();; MigrationStep step3 = new MigrationStep(); if (ue.Count > 0 & ue != null) { string stacdc = "1"; string token4 = GetToken(stacdc); string stacd1 = "2"; string token3 = GetToken(stacd1); for (int i = 0; i < ue.Count; i++) { MapMenUser ftuser = new MapMenUser(); UserInfoItem fuser = step3.GetUserByEmail(token3, ue[i].femail); UserInfoItem tuser = step3.GetUserByEmail(token4, ue[i].temail); ftuser.fid = fuser.id; ftuser.fdisname = fuser.displayName; ftuser.femail = fuser.mail; ftuser.tid = tuser.id; ftuser.tdisname = tuser.displayName; ftuser.temail = tuser.mail; ftemail.Add(ftuser); } } if (!String.IsNullOrEmpty(token7)) { GetChatContent step2 = new GetChatContent(); List <MesageItem> messages = step2.GetChanelMessages(token7, fid, c_id); Comparison <MesageItem> comparison = new Comparison <MesageItem>((MesageItem x, MesageItem y) => { if (x.id < y.id) { return -1; } else if (x.id == y.id) { return 0; } else { return 1; } }); messages.Sort(comparison); for (int i = 0; i < messages.Count; i++) { string user1 = step2.GetUserEmail(token7, messages[i]); string usr1 = GetUsrMapemail(user1); string token3 = GetUsrToken(usr1); string oneaccess = ""; if (String.IsNullOrEmpty(token3)) { string sta_cd = "1"; con.Open(); SqlCommand cmd2 = new SqlCommand("select_token", con); cmd2.CommandType = CommandType.StoredProcedure; cmd2.Parameters.AddWithValue("@sta_cd", sta_cd); using (SqlDataReader reader = cmd2.ExecuteReader()) { if (reader.Read()) { token1 = reader["access_token"].ToString(); ppwd = reader["p_pwd"].ToString(); cid = reader["c_id"].ToString(); cs = reader["c_cs"].ToString(); tid = reader["t_id"].ToString(); } } con.Close(); oneaccess = step3.GetOneAccessToken(usr1, ppwd, cid, cs, tid); } else { oneaccess = token3; } MesageItem mesaft = step3.ClearfyContent(messages[i]); var res = step2.SendMessageDe(oneaccess, ttid, tcid, mesaft, ftemail); if (res == null) { System.Windows.Forms.MessageBox.Show("One message throw an error"); continue; } long new_m_id = res.id; List <MesageItem> replies = step2.GetMessagesReply(token7, fid, c_id, messages[i].id.ToString()); if (replies.Count > 0) { Comparison <MesageItem> comparison1 = new Comparison <MesageItem>((MesageItem x, MesageItem y) => { if (x.id < y.id) { return -1; } else if (x.id == y.id) { return 0; } else { return 1; } }); replies.Sort(comparison1); foreach (MesageItem reply in replies) { MesageItem newreply = step3.ClearfyContent(reply); long mesid = new_m_id; string user2 = step2.GetUserEmail(token7, reply); string usr2 = GetUsrMapemail(user2); string token6 = GetUsrToken(usr2); string oaccess = ""; if (String.IsNullOrEmpty(token6)) { string sta_cd = "1"; con.Open(); SqlCommand cmd2 = new SqlCommand("select_token", con); cmd2.CommandType = CommandType.StoredProcedure; cmd2.Parameters.AddWithValue("@sta_cd", sta_cd); using (SqlDataReader reader = cmd2.ExecuteReader()) { if (reader.Read()) { token1 = reader["access_token"].ToString(); ppwd = reader["p_pwd"].ToString(); cid = reader["c_id"].ToString(); cs = reader["c_cs"].ToString(); tid = reader["t_id"].ToString(); } } con.Close(); oaccess = step3.GetOneAccessToken(usr2, ppwd, cid, cs, tid); } else { oaccess = token6; } if (ftemail.Count > 0 & ftemail != null) { if (!step2.ReplyMessageDe(oaccess, ttid, tcid, mesid, newreply, ftemail)) { System.Windows.Forms.MessageBox.Show("Send a reply error"); continue; } } else { System.Windows.Forms.MessageBox.Show("Access token expired, please sign in again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); Thread.Sleep(200); var account = await App.PublicClientApp.GetAccountsAsync(); if (account.Any()) { try { await App.PublicClientApp.RemoveAsync(account.FirstOrDefault()); con.Open(); SqlCommand cmd1 = new SqlCommand("user_del", con); cmd1.CommandType = CommandType.StoredProcedure; cmd1.ExecuteNonQuery(); con.Close(); ThirdPage th = new ThirdPage(); th.Show(); this.Hide(); } catch (MsalException ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } else { ThirdPage th = new ThirdPage(); th.Show(); this.Hide(); } break; } } } int c = i + 1; SetprogressBar1(c, messages.Count); Thread.Sleep(5); } con.Open(); SqlCommand cmd6 = new SqlCommand("upt_map", con); cmd6.CommandType = CommandType.StoredProcedure; cmd6.Parameters.AddWithValue("@map_id", map_id); cmd6.Parameters.AddWithValue("@cnt_mes", ""); cmd6.ExecuteNonQuery(); con.Close(); } else { System.Windows.Forms.MessageBox.Show("Access token expired, please sign in again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); Thread.Sleep(200); var account = await App.PublicClientApp.GetAccountsAsync(); if (account.Any()) { try { await App.PublicClientApp.RemoveAsync(account.FirstOrDefault()); con.Open(); SqlCommand cmd1 = new SqlCommand("user_del", con); cmd1.CommandType = CommandType.StoredProcedure; cmd1.ExecuteNonQuery(); con.Close(); ThirdPage th = new ThirdPage(); th.Show(); this.Hide(); } catch (MsalException ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } else { ThirdPage th = new ThirdPage(); th.Show(); this.Hide(); } } })); }