CHECK USER IN ACTIVE DIRRECTORY GROUP C#
public static bool EnsureUserInADGroup(string groupName, string
userName)
{
bool retValue = false;
try
{
userName =
userName.Replace(Constants.YZA+ "\\", "");
PrincipalContext ctx = new
PrincipalContext(ContextType.Domain, Constants.YZA);
UserPrincipal user =
UserPrincipal.FindByIdentity(ctx, userName);
GroupPrincipal grp =
GroupPrincipal.FindByIdentity(ctx, groupName);
if (user != null
&& grp != null)
retValue = grp.GetMembers(true).Contains(user);
grp.Dispose();
}
catch (Exception e1)
{ retValue = false; }
return retValue;
}
public static UserPrincipal GetUserDetailsPrincipal(string userName)
{
using (var
context = new PrincipalContext(ContextType.Domain))
{
var usr = UserPrincipal.FindByIdentity(context, userName);
return usr;
}
}
in helper.cs
where you want emp
id
UserPrincipal
userPrincipal = Helper.GetUserDetailsPrincipal(User.Identity.Name);
userPrincipal.EmployeeId
WINDOWS ACTIVE DIRRECTORY PROPERTIES.
[HttpPost]
[Route("GetUserDetails")]
public ADAttributes GetUserDetails(string empID)
{
//string username = User.Identity.Name;
//username = empID;
//// Create a PrincipalContext for the domain
//using (var context = new PrincipalContext(ContextType.Domain, "XYZ.com"))
//{
// // Find the user principal by their identity
// var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username.Split('\\')[1]);
// if (user != null)
// {
// // Retrieve desired properties
// return new AdUserDetails
// {
// SamAccountName = user.SamAccountName,
// DisplayName = user.DisplayName,
// EmailAddress = user.EmailAddress
// };
// }
// else
// return new AdUserDetails();
//}
//---------------
empID = User.Identity.Name;
using (var context = new PrincipalContext(ContextType.Domain, "XYZ"))
{
var user = UserPrincipal.FindByIdentity(context, empID);
if (user != null)
{
//userProperties.EmpName = user.Name;
DirectoryEntry sresult = user.GetUnderlyingObject() as DirectoryEntry;
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
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,
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,
co = sresult.Properties["co"].Count > 0 ? sresult.Properties["co"][0].ToString() : string.Empty
};
return objAd;
}
else
return new ADAttributes();
}
}
}
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 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; }
}
No comments:
Post a Comment