using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using CommonAD.Models; using System.Net.Http.Json; namespace CommonAD { public class WebAPIUtil { /// /// SearchStaff /// /// "http://virtapp-mdb048/CommonAPI/api/AD/" /// "SearchStaff" /// /// /// public static MyADObject SearchStaff(string webAPIUri, string actionEntry, string username) { MyADObject result = null; if (!webAPIUri.EndsWith("/")) webAPIUri += "/"; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var client = new HttpClient()) { client.BaseAddress = new Uri(webAPIUri); // We want the response to be JSON. // client.DefaultRequestHeaders.Accept.Clear(); // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Build up the data to POST. // List> postData = new List>(); // postData.Add(new KeyValuePair("StaffLinkNo", username)); // postData.Add(new KeyValuePair("pass", password)); //postData.Add(new KeyValuePair("client_secret", clientSecret)); //var form = new Dictionary //{ // {"grant_type", "client_credentials"}, //}; //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password))); // HttpContent content = new StringContent(json, Encoding.UTF8); // actionEntry = webAPIUri + actionEntry; // FormUrlEncodedContent content = new FormUrlEncodedContent(postData); //FormUrlEncodedContent content = new FormUrlEncodedContent(form); // Post to the Server and parse the response. //HttpResponseMessage response = await client.PostAsync("Token", content); // string jsonString = await response.Content.ReadAsStringAsync(); //HttpResponseMessage response = client.PostAsync(actionEntry, content).GetAwaiter().GetResult(); actionEntry = actionEntry + "?StaffLinkNo=" + username; try { var myado = client.GetFromJsonAsync(actionEntry).GetAwaiter().GetResult(); if (myado != null) result = myado; } catch(Exception ex) { throw ex; } } return result; } /// /// SearchStaff Async version /// /// "http://virtapp-mdb048/CommonAPI/api/AD/" /// "SearchStaff" /// /// public static async Task SearchStaffAsync(string webAPIUri, string actionEntry, string username) { MyADObject result = null; string error = ""; if (!webAPIUri.EndsWith("/")) webAPIUri += "/"; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var client = new HttpClient()) { client.BaseAddress = new Uri(webAPIUri); // We want the response to be JSON. // client.DefaultRequestHeaders.Accept.Clear(); // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Build up the data to POST. // List> postData = new List>(); // postData.Add(new KeyValuePair("StaffLinkNo", username)); // postData.Add(new KeyValuePair("pass", password)); //postData.Add(new KeyValuePair("client_secret", clientSecret)); //var form = new Dictionary //{ // {"grant_type", "client_credentials"}, //}; //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password))); // HttpContent content = new StringContent(json, Encoding.UTF8); // actionEntry = webAPIUri + actionEntry; //FormUrlEncodedContent content = new FormUrlEncodedContent(postData); //FormUrlEncodedContent content = new FormUrlEncodedContent(form); // Post to the Server and parse the response. //HttpResponseMessage response = await client.PostAsync("Token", content); // string jsonString = await response.Content.ReadAsStringAsync(); //HttpResponseMessage response = client.PostAsync(actionEntry, content).GetAwaiter().GetResult(); actionEntry = actionEntry + "?StaffLinkNo=" + username; try { result = await client.GetFromJsonAsync(actionEntry); } catch (Exception ex) { error = ex.ToString(); result = null; } } return result; } /// /// GetLoginAD webAPIUri "http://virtapp-mdb048/CommonAPI/api/AD/"; /// /// "http://virtapp-mdb048/CommonAPI/api/AD/" /// "CheckADCredentials" /// /// /// /// public static string GetLoginAD(string webAPIUri, string actionEntry, string username, string password, out string error) { string result = ""; // string tokenstr = ""; if (!webAPIUri.EndsWith("/")) webAPIUri += "/"; string mytoken = ""; error = ""; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var client = new HttpClient()) { client.BaseAddress = new Uri(webAPIUri); // We want the response to be JSON. // client.DefaultRequestHeaders.Accept.Clear(); // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Build up the data to POST. // List> postData = new List>(); // postData.Add(new KeyValuePair("user", username)); // postData.Add(new KeyValuePair("pass", password)); //postData.Add(new KeyValuePair("client_secret", clientSecret)); //var form = new Dictionary //{ // {"grant_type", "client_credentials"}, //}; //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password))); // HttpContent content = new StringContent(json, Encoding.UTF8); // actionEntry = webAPIUri + actionEntry; // FormUrlEncodedContent content = new FormUrlEncodedContent(postData); //FormUrlEncodedContent content = new FormUrlEncodedContent(form); // Post to the Server and parse the response. //HttpResponseMessage response = await client.PostAsync("Token", content); // string jsonString = await response.Content.ReadAsStringAsync(); //HttpResponseMessage response = client.PostAsync(actionEntry, content).GetAwaiter().GetResult(); actionEntry = actionEntry + "?user=" + username + "&pass=" + password; //client.GetFromJsonAsync(actionEntry); HttpResponseMessage response = client.GetAsync(actionEntry).GetAwaiter().GetResult(); if (response.StatusCode == HttpStatusCode.OK) { mytoken = (response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); result = mytoken; } else { result = response.ReasonPhrase.ToString(); error = result; result = ""; } } return result; } /// /// GetLoginAD Async version /// /// "http://virtapp-mdb048/CommonAPI/api/AD/" /// "CheckADCredentials" /// /// /// public static async Task GetLoginADAsync(string webAPIUri, string actionEntry, string username, string password) { MyADObject result = null; if (!webAPIUri.EndsWith("/")) webAPIUri += "/"; string error = ""; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var client = new HttpClient()) { client.BaseAddress = new Uri(webAPIUri); // We want the response to be JSON. // client.DefaultRequestHeaders.Accept.Clear(); // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Build up the data to POST. // List> postData = new List>(); // postData.Add(new KeyValuePair("user", username)); // postData.Add(new KeyValuePair("pass", password)); //postData.Add(new KeyValuePair("client_secret", clientSecret)); //var form = new Dictionary //{ // {"grant_type", "client_credentials"}, //}; //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password))); // HttpContent content = new StringContent(json, Encoding.UTF8); // actionEntry = webAPIUri + actionEntry; // FormUrlEncodedContent content = new FormUrlEncodedContent(postData); //FormUrlEncodedContent content = new FormUrlEncodedContent(form); // Post to the Server and parse the response. //HttpResponseMessage response = await client.PostAsync("Token", content); // string jsonString = await response.Content.ReadAsStringAsync(); //HttpResponseMessage response = client.PostAsync(actionEntry, content).GetAwaiter().GetResult(); actionEntry = actionEntry + "?user=" + username + "&pass=" + password; try { result = await client.GetFromJsonAsync(actionEntry); } catch (Exception ex) { error = ex.ToString(); result = null; } } return result; } public static async Task PostLoginADAsync(string webAPIUri, string actionEntry, string username, string password) { MyADObject result = null; if (!webAPIUri.EndsWith("/")) webAPIUri += "/"; string error = ""; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var client = new HttpClient()) { client.BaseAddress = new Uri(webAPIUri + actionEntry); string json = "{\"userName\":\"" + username + "\", \"password\":\"" + password + "\"}"; //var form = new Dictionary //{ // {"grant_type", "client_credentials"}, //}; //client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password))); HttpContent content = new StringContent(json, Encoding.UTF8, "application/json"); // actionEntry = webAPIUri + actionEntry; // FormUrlEncodedContent content = new FormUrlEncodedContent(postData); //FormUrlEncodedContent content = new FormUrlEncodedContent(form); // Post to the Server and parse the response. //HttpResponseMessage response = await client.PostAsync("Token", content); // string jsonString = await response.Content.ReadAsStringAsync(); //HttpResponseMessage response = client.PostAsync(actionEntry, content).GetAwaiter().GetResult(); // actionEntry = actionEntry + "?user=" + username + "&pass=" + password; try { var response = client.PostAsync("",content); string myreturn = await response.Result.Content.ReadAsStringAsync(); result = System.Text.Json.JsonSerializer.Deserialize(myreturn); } catch (Exception ex) { error = ex.ToString(); result = null; } } return result; } } }