//Get User Details Based on NTID from AD
/// <summary>
/// Logged in USER NTID
/// </summary>
public string LogOnName
{
get
{
return SPContext.Current.Site.RootWeb.CurrentUser.LoginName;
}
}
/// <summary>
/// Get User Details Based on NTID
/// </summary>
/// <param name="ntid"></param>
/// <returns></returns>
public Requestor GetRequestorDetails(string ntid)
{
try
{
Requestor objRequestorDetails = new Requestor();
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (string.IsNullOrEmpty(ntid))
{
ntid = LogOnName;
}
//string connection = "LDAP://DC=bp1,DC=com";
using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
string connection = Convert.ToString(Constants.Configuration.RequestorLdapConnection);
string userid = Convert.ToString(Constants.Configuration.ServiceAccountId);
string pwd= Convert.ToString(Constants.GmdmConfiguration.ServiceAccountPassword);
using (var domainContext = new PrincipalContext(ContextType.Domain, connection, Convert.ToString(userid), Convert.ToString(pwd)))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, ntid))
{
if (foundUser != null)
{
try
{
DirectoryEntry directoryEntry = foundUser.GetUnderlyingObject() as DirectoryEntry;
objRequestorDetails.Ntid = ntid;
if (directoryEntry != null)
{
objRequestorDetails.RequestorName = directoryEntry.Properties[Constants.DictionaryVals.displayName][0].ToString();
objRequestorDetails.Email = directoryEntry.Properties[Constants.DictionaryVals.mail][0].ToString();
}
//objRequestorDetails.Country = directoryEntry.Properties["co"][0].ToString();
//objRequestorDetails.PhoneNumber = directoryEntry.Properties["mobile"][0].ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
});
return objRequestorDetails;
}
catch (Exception ex) { GMDMLogger.ErrorloggerUls(Constants.UlsLogs.EntityClassNames.Gmdmservercontext, Constants.UlsLogs.EntityMethodNames.Getrequestordetails, ex); return null; }
}
//User Details Class
public class Requestor{
/// <summary>
/// Requestor Details Properties
/// </summary>
public string Ntid { get; set; }
public string RequestorName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Country { get; set; }
public string Department { get; set; }
}
No comments:
Post a Comment