public string GetOneAccessToken(string user, string pwd, string cid, string cs, string tid) { string token = ""; string CLIENT_ID = ""; string CLIENT_SECERET = ""; string TENAT_ID = ""; string TOKEN_ENDPOINT = ""; string MS_GRAPH_SCOPE = ""; string GRANT_TYPE = ""; string USERNAME = ""; string PASSWORD = ""; try { CLIENT_ID = cid; CLIENT_SECERET = cs; TENAT_ID = tid; // CLIENT_ID = "db9ad5c4-86be-4266-aacb-0552f5bf1cc4"; //CLIENT_SECERET = "VOY6w]I/00Je/7tBuaatcHyjoajr_/.s"; //TENAT_ID = "4dd770a9-6973-4a56-b6a1-89897496fa2d"; TOKEN_ENDPOINT = "https://login.microsoftonline.com/" + TENAT_ID + "/oauth2/v2.0/token"; MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default"; GRANT_TYPE = "password"; USERNAME = user; PASSWORD = pwd; } catch (Exception e) { MessageBox.Show("A error happened while search config file, " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } try { HttpWebRequest request = WebRequest.Create(TOKEN_ENDPOINT) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; StringBuilder data = new StringBuilder(); data.Append("client_id=" + HttpUtility.UrlEncode(CLIENT_ID)); data.Append("&scope=" + HttpUtility.UrlEncode(MS_GRAPH_SCOPE)); data.Append("&client_secret=" + HttpUtility.UrlEncode(CLIENT_SECERET)); data.Append("&GRANT_TYPE=" + HttpUtility.UrlEncode(GRANT_TYPE)); data.Append("&userName="******"&password="******"client_id=" + CLIENT_ID+ "&scope="+ MS_GRAPH_SCOPE+ "&client_secret"+ CLIENT_SECERET+ "&grant_type"+ GRANT_TYPE+ "&userName"+ USERNAME+ "&password"+PASSWORD; byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); request.ContentLength = byteData.Length; using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // Get response using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (var reader = new StreamReader(response.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); LgObject myojb = (LgObject)js.Deserialize(objText, typeof(LgObject)); token = myojb.access_token; } } return(token); } catch (Exception e) { MessageBox.Show("A error happened while connect to server please check config file, " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return("error"); } }
private string GetAccessToken(string ppwd, string clt_id, string clt_cs, string clt_ten, string pusr) { string token = ""; string CLIENT_ID = ""; string CLIENT_SECERET = ""; string TENAT_ID = ""; string TOKEN_ENDPOINT = ""; string MS_GRAPH_SCOPE = ""; string GRANT_TYPE = ""; string USERNAME = ""; string PASSWORD = ""; try { /* * string CLIENT_ID = "db9ad5c4-86be-4266-aacb-0552f5bf1cc4"; * string CLIENT_SECERET = "VOY6w]I/00Je/7tBuaatcHyjoajr_/.s"; * string TENAT_ID = "4dd770a9-6973-4a56-b6a1-89897496fa2d"; * string TOKEN_ENDPOINT = "https://login.microsoftonline.com/" + TENAT_ID + "/oauth2/v2.0/token"; * string MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default"; * string GRANT_TYPE = "password"; * string USERNAME = "******"; * string PASSWORD = "******"; */ CLIENT_ID = clt_id; CLIENT_SECERET = clt_cs; TENAT_ID = clt_ten; TOKEN_ENDPOINT = "https://login.microsoftonline.com/" + TENAT_ID + "/oauth2/v2.0/token"; MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default"; GRANT_TYPE = "password"; USERNAME = pusr; PASSWORD = ppwd; } catch (Exception e) { System.Windows.Forms.MessageBox.Show("A error happened while search config file " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } try { HttpWebRequest request = WebRequest.Create(TOKEN_ENDPOINT) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; StringBuilder data = new StringBuilder(); data.Append("client_id=" + HttpUtility.UrlEncode(CLIENT_ID)); data.Append("&scope=" + HttpUtility.UrlEncode(MS_GRAPH_SCOPE)); data.Append("&client_secret=" + HttpUtility.UrlEncode(CLIENT_SECERET)); data.Append("&GRANT_TYPE=" + HttpUtility.UrlEncode(GRANT_TYPE)); data.Append("&userName="******"&password="******"client_id=" + CLIENT_ID+ "&scope="+ MS_GRAPH_SCOPE+ "&client_secret"+ CLIENT_SECERET+ "&grant_type"+ GRANT_TYPE+ "&userName"+ USERNAME+ "&password"+PASSWORD; byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); request.ContentLength = byteData.Length; using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // Get response using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (var reader = new StreamReader(response.GetResponseStream())) { string sta_cd = "2"; System.Web.Script.Serialization.JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); LgObject myojb = (LgObject)js.Deserialize(objText, typeof(LgObject)); token = myojb.access_token; con.Open(); SqlCommand cmd = new SqlCommand("addUser", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@email", pusr); cmd.Parameters.AddWithValue("@p_pwd", ppwd); cmd.Parameters.AddWithValue("@c_id", CLIENT_ID); cmd.Parameters.AddWithValue("@c_cs", CLIENT_SECERET); cmd.Parameters.AddWithValue("@t_id", clt_ten); cmd.Parameters.AddWithValue("@access_token", token); cmd.Parameters.AddWithValue("@sta_cd", sta_cd); cmd.ExecuteNonQuery(); con.Close(); } } return(token); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("A error happened while connect to server please check config file " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return("error"); } }