317 lines
15 KiB
C#
317 lines
15 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// SearchStaff
|
|
/// </summary>
|
|
/// <param name="webAPIUri">"http://virtapp-mdb048/CommonAPI/api/AD/"</param>
|
|
/// <param name="actionEntry"> "SearchStaff"</param>
|
|
/// <param name="username"></param>
|
|
/// <param name="error"></param>
|
|
/// <returns></returns>
|
|
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<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
|
|
// postData.Add(new KeyValuePair<string, string>("StaffLinkNo", username));
|
|
// postData.Add(new KeyValuePair<string, string>("pass", password));
|
|
//postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
|
|
|
|
//var form = new Dictionary<string, string>
|
|
//{
|
|
// {"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<MyADObject>(actionEntry).GetAwaiter().GetResult();
|
|
if (myado != null)
|
|
result = myado;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// SearchStaff Async version
|
|
/// </summary>
|
|
/// <param name="webAPIUri">"http://virtapp-mdb048/CommonAPI/api/AD/"</param>
|
|
/// <param name="actionEntry"> "SearchStaff"</param>
|
|
/// <param name="username"></param>
|
|
/// <returns></returns>
|
|
public static async Task<MyADObject> 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<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
|
|
// postData.Add(new KeyValuePair<string, string>("StaffLinkNo", username));
|
|
// postData.Add(new KeyValuePair<string, string>("pass", password));
|
|
//postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
|
|
|
|
//var form = new Dictionary<string, string>
|
|
//{
|
|
// {"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<MyADObject>(actionEntry);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
result = null;
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// GetLoginAD webAPIUri "http://virtapp-mdb048/CommonAPI/api/AD/";
|
|
/// </summary>
|
|
/// <param name="webAPIUri"> "http://virtapp-mdb048/CommonAPI/api/AD/"</param>
|
|
/// <param name="actionEntry">"CheckADCredentials"</param>
|
|
/// <param name="username"></param>
|
|
/// <param name="password"></param>
|
|
/// <param name="error"></param>
|
|
/// <returns></returns>
|
|
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<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
|
|
// postData.Add(new KeyValuePair<string, string>("user", username));
|
|
// postData.Add(new KeyValuePair<string, string>("pass", password));
|
|
//postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
|
|
|
|
//var form = new Dictionary<string, string>
|
|
//{
|
|
// {"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<MyADObject>(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;
|
|
}
|
|
/// <summary>
|
|
/// GetLoginAD Async version
|
|
/// </summary>
|
|
/// <param name="webAPIUri"> "http://virtapp-mdb048/CommonAPI/api/AD/"</param>
|
|
/// <param name="actionEntry">"CheckADCredentials"</param>
|
|
/// <param name="username"></param>
|
|
/// <param name="password"></param>
|
|
/// <returns></returns>
|
|
public static async Task<MyADObject> 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<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
|
|
// postData.Add(new KeyValuePair<string, string>("user", username));
|
|
// postData.Add(new KeyValuePair<string, string>("pass", password));
|
|
//postData.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
|
|
|
|
//var form = new Dictionary<string, string>
|
|
//{
|
|
// {"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<MyADObject>(actionEntry);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
result = null;
|
|
}
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static async Task<MyADObject> 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<string, string>
|
|
//{
|
|
// {"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<MyADObject>(myreturn);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
result = null;
|
|
}
|
|
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|