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 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"]));
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,374 @@
|
||||
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 ("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,33 @@
|
||||
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