first time include all file
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Configuration;
|
||||
|
||||
namespace CommonAD.Models
|
||||
{
|
||||
public class ADConfig
|
||||
{
|
||||
public string LDAPPath = "";
|
||||
public string LDAPUser = "";
|
||||
public string LDAPPassword = "";
|
||||
private TimeSpan _ADTimeOut = TimeSpan.FromSeconds(0);
|
||||
|
||||
public TimeSpan ADTimeOut
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ADTimeOut;
|
||||
}
|
||||
}
|
||||
public string ADTimeOutStr
|
||||
{
|
||||
set
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
_ADTimeOut = TimeSpan.FromSeconds(Convert.ToDouble(value));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public static void Init()
|
||||
{
|
||||
LDAPPath = ConfigurationManager.AppSettings["LDAPPath"];
|
||||
LDAPUser = ConfigurationManager.AppSettings["LDAPUser"];
|
||||
LDAPPassword = ConfigurationManager.AppSettings["LDAPPassword"];
|
||||
ADTimeOut = TimeSpan.FromSeconds(Convert.ToDouble(ConfigurationManager.AppSettings["ADTimeOut"]));
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,373 @@
|
||||
using CommonAD.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.DirectoryServices;
|
||||
|
||||
|
||||
namespace CommonAD
|
||||
{
|
||||
public class ADStaffLink
|
||||
{
|
||||
private ADConfig _adConfig;
|
||||
public ADStaffLink (ADConfig adConfig)
|
||||
{
|
||||
_adConfig = adConfig;
|
||||
}
|
||||
public MyADObject SearchStaff(string FirstName = "", string LastName = "")
|
||||
{
|
||||
|
||||
MyADObject ADObj = new MyADObject();
|
||||
|
||||
using (var DE = new System.DirectoryServices.DirectoryEntry(_adConfig.LDAPPath, _adConfig.LDAPUser, _adConfig.LDAPPassword))
|
||||
{
|
||||
try
|
||||
{
|
||||
DirectorySearcher adsSearcher = new DirectorySearcher(DE);
|
||||
adsSearcher.ClientTimeout = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerTimeLimit = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerPageTimeLimit = _adConfig.ADTimeOut;
|
||||
adsSearcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", FirstName, LastName);
|
||||
try
|
||||
{
|
||||
SearchResult sr = adsSearcher.FindOne();
|
||||
if (sr != null)
|
||||
{
|
||||
ADObj.result = true;
|
||||
//LoginResult.Login = GetUserPropertyValue(sr, "sAMAccountName", 0);
|
||||
ADObj.StafflinkNo = GetUserPropertyValue(sr, "sAMAccountName", 0);
|
||||
ADObj.FirstName = GetUserPropertyValue(sr, "givenname", 0);
|
||||
ADObj.LastName = GetUserPropertyValue(sr, "sn", 0);
|
||||
ADObj.Email = GetUserPropertyValue(sr, "mail", 0);
|
||||
ADObj.Company = GetUserPropertyValue(sr, "company", 0);
|
||||
ADObj.Location = GetUserPropertyValue(sr, "physicalDeliveryOfficeName", 0);
|
||||
ADObj.Department = GetUserPropertyValue(sr, "department", 0);
|
||||
ADObj.BadPwdCount = GetUserPropertyValue(sr, "badPwdCount", 0);
|
||||
ADObj.Phone = GetUserPropertyValue(sr, "telephonenumber", 0);
|
||||
ADObj.DisplayName = GetUserPropertyValue(sr, "displayName", 0);
|
||||
ADObj.Manager = GetUserPropertyValue(sr, "manager", 0);
|
||||
ADObj.ReportsTo = GetUserPropertyValue(sr, "directReports", 0);
|
||||
ADObj.StaffList = GetUserPropertyValue(sr, "staffList", 0);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Failed to authenticate. Most likely it is caused by unknown user
|
||||
// id or bad strPassword.
|
||||
ADObj.result = false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
ADObj.result = false;
|
||||
}
|
||||
}
|
||||
|
||||
//LoginResult.Success = res;
|
||||
//string token = "";
|
||||
//long tickTock = DateTime.Now.Ticks;
|
||||
//if (res)
|
||||
//{
|
||||
// string tokenPT = app + "|" + user + "|" + "|" + tickTock + "|" + "NBM";
|
||||
// token = CommonCrypt.EncryptString(tokenPT, "bagmas2011+", "a1b2c3d4");
|
||||
// token = CommonEncoding.Base64Encode(token);
|
||||
//}
|
||||
//LoginResult.Login = user;
|
||||
//LoginResult.Token = token;
|
||||
//LoginResult.Time = tickTock;
|
||||
return ADObj;
|
||||
|
||||
|
||||
}
|
||||
public MyADObject SearchStaff(string StaffLinkNo)
|
||||
{
|
||||
|
||||
MyADObject ADObj = new MyADObject();
|
||||
|
||||
using (var DE = new System.DirectoryServices.DirectoryEntry(_adConfig.LDAPPath, _adConfig.LDAPUser, _adConfig.LDAPPassword))
|
||||
{
|
||||
try
|
||||
{
|
||||
DirectorySearcher adsSearcher = new DirectorySearcher(DE);
|
||||
adsSearcher.ClientTimeout = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerTimeLimit = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerPageTimeLimit = _adConfig.ADTimeOut;
|
||||
adsSearcher.Filter = "(SAMAccountName=" + StaffLinkNo + ")";
|
||||
try
|
||||
{
|
||||
SearchResult sr = adsSearcher.FindOne();
|
||||
if (sr != null)
|
||||
{
|
||||
ADObj.result = true;
|
||||
// LoginResult.Login = GetUserPropertyValue(sr, "sAMAccountName", 0); ;
|
||||
ADObj.StafflinkNo = GetUserPropertyValue(sr, "SAMAccountName", 0);
|
||||
ADObj.FirstName = GetUserPropertyValue(sr, "givenname", 0);
|
||||
ADObj.LastName = GetUserPropertyValue(sr, "sn", 0);
|
||||
ADObj.Email = GetUserPropertyValue(sr, "mail", 0);
|
||||
ADObj.Company = GetUserPropertyValue(sr, "company", 0);
|
||||
ADObj.Location = GetUserPropertyValue(sr, "physicalDeliveryOfficeName", 0);
|
||||
ADObj.Department = GetUserPropertyValue(sr, "department", 0);
|
||||
ADObj.JobTitle = GetUserPropertyValue(sr, "title", 0);
|
||||
ADObj.BadPwdCount = GetUserPropertyValue(sr, "badPwdCount", 0);
|
||||
ADObj.Phone = GetUserPropertyValue(sr, "telephonenumber", 0);
|
||||
ADObj.DisplayName = GetUserPropertyValue(sr, "displayName", 0);
|
||||
ADObj.Manager = GetUserPropertyValue(sr, "manager", 0);
|
||||
ADObj.ReportsTo = GetUserPropertyValue(sr, "directReports", 0);
|
||||
ADObj.StaffList = GetUserPropertyValue(sr, "staffList", 0);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Failed to authenticate. Most likely it is caused by unknown user
|
||||
// id or bad strPassword.
|
||||
ADObj.result = false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
ADObj.result = false;
|
||||
}
|
||||
}
|
||||
|
||||
//LoginResult.Success = res;
|
||||
//string token = "";
|
||||
//long tickTock = DateTime.Now.Ticks;
|
||||
//if (res)
|
||||
//{
|
||||
// string tokenPT = app + "|" + user + "|" + "|" + tickTock + "|" + "NBM";
|
||||
// token = CommonCrypt.EncryptString(tokenPT, "bagmas2011+", "a1b2c3d4");
|
||||
// token = CommonEncoding.Base64Encode(token);
|
||||
//}
|
||||
//LoginResult.Login = user;
|
||||
//LoginResult.Token = token;
|
||||
//LoginResult.Time = tickTock;
|
||||
return ADObj;
|
||||
|
||||
|
||||
}
|
||||
public MyADObject CheckADCredentials(string user, string pass)
|
||||
{
|
||||
//dynamic LoginResult = new JObject() as dynamic;
|
||||
bool res = true;
|
||||
MyADObject ADObj = new MyADObject();
|
||||
|
||||
using (var DE = new System.DirectoryServices.DirectoryEntry(_adConfig.LDAPPath, user, pass))
|
||||
{
|
||||
try
|
||||
{
|
||||
//DE.RefreshCache(); // This will force credentials validation
|
||||
DirectorySearcher adsSearcher = new DirectorySearcher(DE);
|
||||
adsSearcher.ClientTimeout = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerTimeLimit = _adConfig.ADTimeOut;
|
||||
adsSearcher.ServerPageTimeLimit = _adConfig.ADTimeOut;
|
||||
//adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
|
||||
adsSearcher.Filter = "(sAMAccountName=" + user + ")";
|
||||
|
||||
try
|
||||
{
|
||||
SearchResult sr = adsSearcher.FindOne();
|
||||
if (sr != null)
|
||||
{
|
||||
ADObj.StafflinkNo = GetUserPropertyValue(sr, "sAMAccountName", 0); ;
|
||||
ADObj.FirstName = GetUserPropertyValue(sr, "givenname", 0);
|
||||
ADObj.LastName = GetUserPropertyValue(sr, "sn", 0);
|
||||
ADObj.Email = GetUserPropertyValue(sr, "mail", 0);
|
||||
ADObj.Company = GetUserPropertyValue(sr, "company", 0);
|
||||
ADObj.Location = GetUserPropertyValue(sr, "physicalDeliveryOfficeName", 0);
|
||||
ADObj.Department = GetUserPropertyValue(sr, "department", 0);
|
||||
ADObj.BadPwdCount = GetUserPropertyValue(sr, "badPwdCount", 0);
|
||||
ADObj.Phone = GetUserPropertyValue(sr, "telephonenumber", 0);
|
||||
ADObj.DisplayName = GetUserPropertyValue(sr, "displayName", 0);
|
||||
ADObj.Manager = GetUserPropertyValue(sr, "manager", 0);
|
||||
|
||||
// we will retrieve the count of staff in DirectReports as a flag to determine if this user is a manager
|
||||
// we will assume if no staff then requests lodged by this user will need signoff by their manager
|
||||
//LoginResult.StaffListCount = sr.Properties["DirectReports"].Count;
|
||||
//LoginResult.PWD = ADConfig.LDAPPassword;
|
||||
//LoginResult.User = ADConfig.LDAPUser;
|
||||
|
||||
|
||||
/* // we do not think we need actual staff IDs at this point - a count of staff will be enough for this application
|
||||
// but leave here in case we need to re-use this in another app.
|
||||
List<string> StaffList = new List<string>();
|
||||
foreach (string objProperty in sr.Properties["DirectReports"])
|
||||
{
|
||||
|
||||
|
||||
string emp = objProperty.ToString().Split(',')[0];
|
||||
emp = emp.Replace("CN=", "");
|
||||
StaffList.Add(emp);
|
||||
//emps.Add(emp);
|
||||
//user.StaffList += '|';
|
||||
//user.StaffList += emp;
|
||||
//
|
||||
}
|
||||
LoginResult.StaffList = JsonConvert.SerializeObject(StaffList);
|
||||
*/
|
||||
|
||||
try
|
||||
{
|
||||
if (ADObj.Manager != String.Empty)
|
||||
{
|
||||
String managerInfo = Convert.ToString(ADObj.Manager);
|
||||
String managerID = managerInfo.Split(',')[0].Split('=')[1];
|
||||
String managerOU = managerInfo.Split(',')[1].Split('=')[1];
|
||||
List<MyADObject> managerDetails = Search(managerID, managerOU);
|
||||
// JToken managerDetails = Search(managerID);
|
||||
ADObj.ManagerDetails = managerDetails[0].ToString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ADObj.ManagerDetails = "Not available: " + ex.Message;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
// Failed to authenticate. Most likely it is caused by unknown user
|
||||
// id or bad strPassword.
|
||||
res = false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
res = false;
|
||||
}
|
||||
}
|
||||
|
||||
ADObj.result = res;
|
||||
// string token = "";
|
||||
// long tickTock = DateTime.Now.Ticks;
|
||||
// if (res)
|
||||
// {
|
||||
//string tokenPT = app + "|" + user + "|" + pass + "|" + tickTock + "|" + "NBM";
|
||||
//token = CommonCrypt.EncryptString(tokenPT, "bagmas2011+", "a1b2c3d4");
|
||||
//token = CommonEncoding.Base64Encode(token);
|
||||
//}
|
||||
|
||||
//LoginResult.Token = token;
|
||||
//LoginResult.Time = tickTock;
|
||||
|
||||
|
||||
//return LoginResult;
|
||||
return ADObj;
|
||||
|
||||
}
|
||||
public List<MyADObject> Search(string q, string ou)
|
||||
{
|
||||
//ou = "Nepean Blue Mountains";
|
||||
MyADObject ADObj = new Models.MyADObject();
|
||||
//ADConfig.Init();
|
||||
|
||||
List<MyADObject> lstLDAPUser = new List<MyADObject>();
|
||||
|
||||
if (string.IsNullOrEmpty(q))
|
||||
q = "______"; //defualt not found
|
||||
System.DirectoryServices.DirectoryEntry dirEntry = new System.DirectoryServices.DirectoryEntry(_adConfig.LDAPPath, _adConfig.LDAPUser, _adConfig.LDAPPassword);
|
||||
DirectorySearcher dirSearch = new DirectorySearcher(dirEntry);
|
||||
dirSearch.ClientTimeout = _adConfig.ADTimeOut;
|
||||
dirSearch.ServerTimeLimit = _adConfig.ADTimeOut;
|
||||
dirSearch.ServerPageTimeLimit = _adConfig.ADTimeOut;
|
||||
dirSearch.Filter = "(&(objectClass=user)(|(cn=" + q + "*)))";
|
||||
dirSearch.SearchRoot = dirEntry;
|
||||
dirSearch.SearchScope = SearchScope.Subtree;
|
||||
dirSearch.PageSize = 100;
|
||||
dirSearch.Sort = new SortOption("displayName", SortDirection.Ascending);
|
||||
|
||||
|
||||
using (SearchResultCollection src = dirSearch.FindAll())
|
||||
{
|
||||
int i = 0;
|
||||
foreach (SearchResult sr in src)
|
||||
{
|
||||
i++;
|
||||
if (i > 100)
|
||||
break;
|
||||
bool bLogindisabled = true;
|
||||
|
||||
string sTmp = GetUserPropertyValue(sr, "logindisabled", 0);
|
||||
bLogindisabled = (sTmp == string.Empty ? false : Convert.ToBoolean(sTmp));
|
||||
|
||||
if (!bLogindisabled)
|
||||
{
|
||||
sTmp = GetUserPropertyValue(sr, "passwordexpirationtime", 0);
|
||||
if (sTmp != "")
|
||||
{
|
||||
try
|
||||
{
|
||||
if (DateTime.Now > Convert.ToDateTime(sTmp))
|
||||
{
|
||||
bLogindisabled = true;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
if (!bLogindisabled)
|
||||
{
|
||||
MyADObject user = new MyADObject();
|
||||
// user.Login = GetUserPropertyValue(sr, "sAMAccountName", 0);
|
||||
user.StafflinkNo = GetUserPropertyValue(sr, "sAMAccountName", 0);
|
||||
user.FirstName = GetUserPropertyValue(sr, "givenname", 0);
|
||||
user.LastName = GetUserPropertyValue(sr, "sn", 0);
|
||||
user.Email = GetUserPropertyValue(sr, "mail", 0);
|
||||
user.Company = GetUserPropertyValue(sr, "company", 0);
|
||||
user.Location = GetUserPropertyValue(sr, "physicalDeliveryOfficeName", 0);
|
||||
user.Department = GetUserPropertyValue(sr, "department", 0);
|
||||
user.BadPwdCount = GetUserPropertyValue(sr, "badPwdCount", 0);
|
||||
//user.company = GetUserPropertyValue(sr, "company", 0);
|
||||
user.Phone = GetUserPropertyValue(sr, "telephonenumber", 0);
|
||||
user.DisplayName = GetUserPropertyValue(sr, "displayName", 0);
|
||||
//user.distinguishedName = GetUserPropertyValue(sr, "distinguishedName", 0);
|
||||
user.DisplayName = GetUserPropertyValue(sr, "displayName", 0);
|
||||
|
||||
lstLDAPUser.Add(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if (dirSearch != null)
|
||||
dirSearch.Dispose();
|
||||
if (dirEntry != null)
|
||||
dirEntry.Dispose();
|
||||
return lstLDAPUser;
|
||||
|
||||
}//end method
|
||||
private string GetUserPropertyValue(SearchResult res, string sPropertyName, int lIndex)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (res.Properties[sPropertyName].Count > lIndex)
|
||||
{
|
||||
string s;
|
||||
object v = res.Properties[sPropertyName][lIndex];
|
||||
|
||||
if (v.GetType() == typeof(byte[]))
|
||||
{
|
||||
s = System.Text.ASCIIEncoding.ASCII.GetString((byte[])v);
|
||||
}
|
||||
else if (v.GetType() == typeof(DateTime))
|
||||
{
|
||||
s = ((DateTime)v).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");
|
||||
}
|
||||
else
|
||||
{
|
||||
s = v.ToString();
|
||||
}
|
||||
return (s);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ("");
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
|
||||
<LangVersion>14</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="ADStaffLink.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.DirectoryServices" Version="10.0.8" />
|
||||
<PackageReference Include="System.Net.Http.Json" Version="10.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Configuration;
|
||||
|
||||
|
||||
namespace CommonAD.Models
|
||||
{
|
||||
public class MyADObject
|
||||
{
|
||||
public string stafflinkNo { get; set; }
|
||||
public string firstName { get; set; }
|
||||
public string lastName { get; set; }
|
||||
public string email { get; set; }
|
||||
public string company { get; set; }
|
||||
public string location { get; set; }
|
||||
public string department { get; set; }
|
||||
public string jobTitle { get; set; }
|
||||
public string badPwdCount { get; set; }
|
||||
public string phone { get; set; }
|
||||
public string displayName { get; set; }
|
||||
public string manager { get; set; }
|
||||
public string reportsTo { get; set; }
|
||||
public string staffList { get; set; }
|
||||
public string managerDetails { get; set; }
|
||||
public bool result { get; set; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user