commit change first
This commit is contained in:
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.31515.178
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarParkValidationAPI", "CarParkValidationAPI\CarParkValidationAPI.csproj", "{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarParkValidationAPI", "CarParkValidationAPI\CarParkValidationAPI.csproj", "{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonAD", "CommonAD\CommonAD.csproj", "{43FC73FD-1EE6-4B68-9299-E77049B5E490}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonAD", "..\CommonAD\CommonAD.csproj", "{0E8073C3-27BC-4E2E-B13B-E8692835BA9C}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -17,10 +17,10 @@ Global
|
|||||||
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Release|Any CPU.Build.0 = Release|Any CPU
|
{FCD8F53F-967F-4E66-B42C-DCD0C83B2670}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{43FC73FD-1EE6-4B68-9299-E77049B5E490}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{0E8073C3-27BC-4E2E-B13B-E8692835BA9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{43FC73FD-1EE6-4B68-9299-E77049B5E490}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0E8073C3-27BC-4E2E-B13B-E8692835BA9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{43FC73FD-1EE6-4B68-9299-E77049B5E490}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0E8073C3-27BC-4E2E-B13B-E8692835BA9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{43FC73FD-1EE6-4B68-9299-E77049B5E490}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0E8073C3-27BC-4E2E-B13B-E8692835BA9C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\CommonAD\CommonAD.csproj" />
|
<ProjectReference Include="..\..\CommonAD\CommonAD.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
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"]));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,373 +0,0 @@
|
|||||||
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 ("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="System.DirectoryServices" Version="5.0.0" />
|
|
||||||
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
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; }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,268 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Web;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace CommonApi.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"]));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,374 +0,0 @@
|
|||||||
using CommonApi.Models;
|
|
||||||
using CommonAuth.Models;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.DirectoryServices;
|
|
||||||
|
|
||||||
|
|
||||||
namespace CommonAuth
|
|
||||||
{
|
|
||||||
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 ("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="System.DirectoryServices" Version="5.0.0" />
|
|
||||||
<PackageReference Include="System.DirectoryServices.Protocols" Version="5.0.0" />
|
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Web;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
|
|
||||||
namespace CommonAuth.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; }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Web;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace CommonApi.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"]));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,374 +0,0 @@
|
|||||||
using CommonApi.Models;
|
|
||||||
using CommonAuth.Models;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.DirectoryServices;
|
|
||||||
using System.Reflection.PortableExecutable;
|
|
||||||
|
|
||||||
namespace CommonAuth
|
|
||||||
{
|
|
||||||
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 ("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="System.DirectoryServices" Version="5.0.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Web;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
|
|
||||||
namespace CommonAuth.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; }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user