GET THE MANAGER DETAILS AND CURRENT LOGIN USER DETAILS FROM ACTIVE DIRECTORY
outlook properties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
public partial class MgrFromAD : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ADAttributes ad = new ADAttributes();
ad = GetEmployeeAttributes("Ashwini Sharma",
AdPrpoertyParameters.displayName);
}
}
public ADAttributes GetEmployeeAttributes(string Input, AdPrpoertyParameters PropertyParameter)
{
ADAttributes attributes = null;
try
{
string strLDAPPath = "LDAP://DC=at,DC=com"; // configurable
attributes =
DirectoryEntryforAttributes(strLDAPPath, PropertyParameter.ToString(), Input, "AT");
//if (attributes == null)
//{
// attributes =
DirectoryEntryforAttributes(strLDAPPath, PropertyParameter.ToString(), Input,
CONST_TEL);
//}
ADAttributes mgrAttributes = null;
mgrAttributes =
DirectoryEntryforAttributes(strLDAPPath, AdPrpoertyParameters.sAMAccountName.ToString(),
attributes.managerSamAcnt, "AT");
//if (mgrAttributes == null)
//{
// mgrAttributes
= DirectoryEntryforAttributes(strLDAPPath,
AdPrpoertyParameters.sAMAccountName.ToString(), attributes.managerSamAcnt,
CONST_TEL);
//}
if
(mgrAttributes != null)
{
attributes.ManagerEmail =
mgrAttributes.mail;
attributes.ManagerLoginName =
mgrAttributes.sAMAccountName;
attributes.ManagerDisplayName =
mgrAttributes.displayName;
attributes.ManagerDomain =
mgrAttributes.domain;
attributes.ManagerEmployeeID =
mgrAttributes.employeeId;
attributes.ManagerPhoneNumber =
mgrAttributes.telephoneNumber;
}
}
catch (Exception ex)
{
throw
ex;
}
return attributes;
}
public ADAttributes DirectoryEntryforAttributes(string PropertyKey, string Filtervalue, string Input, string DomainName)
{
SearchResult sresult;
string query = string.Empty;
string ldappath = "";
DirectoryEntry entry_Amat = new DirectoryEntry(ldappath);
DirectorySearcher userSearch_Amat = new DirectorySearcher(entry_Amat);
query = string.Format("(&(objectClass=user)(" + Filtervalue + "={0}))", Input);
userSearch_Amat.Filter = query;
//Remove this code
if not required
sresult = userSearch_Amat.FindOne();
if (sresult == null)
{
query = string.Format("(&(objectClass=contact)(" + Filtervalue + "={0}))", Input);
userSearch_Amat.Filter = query;
//samSearcher.PropertiesToLoad.Add(Filtervalue);
sresult = userSearch_Amat.FindOne();
}
ADAttributes objAd = ADAttributes.GetDirectoryAttributes(sresult,
Filtervalue, Input, DomainName);
return objAd;
}
}
public class ADAttributes
{
public string employeeId { get; set; }
public string employeeType { get; set; }
public string mail { get; set; }
public string sAMAccountName { get; set; }
public string displayName { get; set; }
public string givenName { get; set; }
public string manager { get; set; }
public string memberOf { get; set; }
public string department { get; set; }
public string departmentNumber { get; set; }
public string division { get; set; }
public string userAccountControl { get; set; }
public string telephoneNumber { get; set; }
public string postOfficeBox { get; set; }
public string PhysicalDeliveryOfficeName {
get; set; }
public string objectClass { get; set; }
public string uId { get; set; }
public string domain { get; set; }
public string Location { get; set; }
public string Company { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// public string ManagerEmailAddress { get;
set; }
public string managerSamAcnt { get; set; }
public string ManagerEmail { get; set; }
public string ManagerLoginName { get; set; }
public string ManagerDisplayName { get; set; }
public string ManagerDomain { get; set; }
public string co { get; set; }
public string ManagerEmployeeID { get; set; }
public string ManagerPhoneNumber { get; set; }
public static ADAttributes GetDirectoryAttributes(SearchResult sresult, string Filtervalue, string Input, string DomainName)
{
if (sresult != null)
{
ADAttributes objAd = new ADAttributes
{
employeeId =
sresult.Properties["employeeid"].Count > 0 ? sresult.Properties["employeeid"][0].ToString() : string.Empty,
employeeType =
sresult.Properties["employeeType"].Count > 0 ? sresult.Properties["employeeType"][0].ToString() : string.Empty,
sAMAccountName =
sresult.Properties["sAMAccountName"].Count > 0 ? sresult.Properties["sAMAccountName"][0].ToString() : string.Empty,
department = sresult.Properties["department"].Count > 0 ?
sresult.Properties["department"][0].ToString() : string.Empty,
departmentNumber =
sresult.Properties["departmentNumber"].Count > 0 ? sresult.Properties["departmentNumber"][0].ToString() : string.Empty,
division =
sresult.Properties["division"].Count > 0 ? sresult.Properties["division"][0].ToString() : string.Empty,
manager = sresult.Properties["manager"].Count > 0 ?
sresult.Properties["manager"][0].ToString()
: string.Empty,
managerSamAcnt =
(sresult.Properties.Contains("manager") == true) ? Convert.ToString(sresult.Properties["manager"][0]).Split(',')[0].Split('=')[1] : string.Empty, // Splitting manger name
//manager =
(sresult.Properties.Contains("manager") == true) ?
Convert.ToString(sresult.Properties["manager"][0]).Split(',')[0].Split('=')[1]
: string.Empty, // Splitting manger name
displayName =
sresult.Properties["displayName"].Count > 0 ? sresult.Properties["displayName"][0].ToString() : string.Empty,
PhysicalDeliveryOfficeName =
sresult.Properties["physicalDeliveryOfficeName"].Count > 0 ? sresult.Properties["physicalDeliveryOfficeName"][0].ToString() : string.Empty,
mail =
sresult.Properties["mail"].Count
> 0 ? sresult.Properties["mail"][0].ToString() : string.Empty,
memberOf = sresult.Properties["memberOf"].Count > 0 ?
sresult.Properties["memberOf"][0].ToString() : string.Empty,
//memberOf =
(sresult.Properties["memberOf"].Count > 0) ?
Convert.ToString(sresult.Properties["memberOf"][0]).Split(',')[0].Split('=')[1]
: string.Empty, //Splitting manger name
userAccountControl =
sresult.Properties["userAccountControl"].Count > 0 ? sresult.Properties["userAccountControl"][0].ToString() : string.Empty,
givenName = sresult.Properties["givenName"].Count > 0 ?
sresult.Properties["givenName"][0].ToString() : string.Empty,
objectClass = sresult.Properties["objectClass"].Count > 0 ?
sresult.Properties["objectClass"][0].ToString() : string.Empty,
postOfficeBox =
sresult.Properties["postOfficeBox"].Count > 0 ? sresult.Properties["postOfficeBox"][0].ToString() : string.Empty,
telephoneNumber =
sresult.Properties["telephoneNumber"].Count > 0 ? sresult.Properties["telephoneNumber"][0].ToString() : string.Empty,
uId = sresult.Properties["uId"].Count > 0 ?
sresult.Properties["uId"][0].ToString()
: string.Empty,
Location = sresult.Properties["L"].Count > 0 ?
sresult.Properties["L"][0].ToString()
: string.Empty,
Company = sresult.Properties["Company"].Count > 0 ?
sresult.Properties["Company"][0].ToString()
: string.Empty,
FirstName = sresult.Properties["givenName"].Count > 0 ?
sresult.Properties["givenName"][0].ToString() : string.Empty,
LastName = sresult.Properties["sn"].Count > 0 ?
sresult.Properties["sn"][0].ToString()
: string.Empty,
//
ManagerEmailAddress =
sresult.Properties["ManagerEmailAddress"].Count > 0 ?
sresult.Properties["ManagerEmailAddress"][0].ToString() :
string.Empty,
co = sresult.Properties["co"].Count > 0 ?
sresult.Properties["co"][0].ToString()
: string.Empty,
domain = DomainName
};
return objAd;
}
else
{
return null;
}
}
}
/// <summary>
/// Enum AD properties
/// </summary>
public enum AdPrpoertyParameters
{
employeeid,
sAMAccountName,
displayName,
mail,
CN // for current login user identity name from System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Replace("AMT\\", "")
CN // for current login user identity name from System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Replace("AMT\\", "")
}
No comments:
Post a Comment