public void TestMethod3()
 {
     WindowsIdentity wi = WindowsIdentity.GetCurrent();
     Assert.IsNotNull(wi);
     AdSearch adSearch = new AdSearch(AdSearch.GetDomainFromUserAccount(wi.Name));
     Assert.IsNotNull(adSearch.FindEmail(wi.Name));
 }
 private string GetEmailAddress(JobExecutionContext context, WindowsIdentity windowsIdentity)
 {
    JobDataMap jobDataMap = context.MergedJobDataMap;
    string email;
    if (jobDataMap.Contains(KeyEmail))
    {
        email = Convert.ToString(jobDataMap[KeyEmail]);
    }
    else
    {
       AdSearch adSearch = new AdSearch(AdSearch.GetDomainFromUserAccount(windowsIdentity.Name));
       email = adSearch.FindEmail(AdSearch.GetAccountNameFromUserAccount(windowsIdentity.Name));
    }
    return email;
 }
 public WindowsIdentity GetWindowsIdentity(JobExecutionContext context)
 {
      string userPrincipalName = null;
      string userAccount = null;
      JobDataMap jobDataMap = context.MergedJobDataMap;
      if (jobDataMap.Contains(KeyUserAccount))
      {
          userAccount = Convert.ToString(jobDataMap[KeyUserAccount]);
      }
      if (String.IsNullOrEmpty(userAccount) || userAccount.Trim().Length == 0)
      {
          return WindowsIdentity.GetCurrent();
      }
      string domainName = AdSearch.GetDomainFromUserAccount(userAccount);
      AdSearch adSearch = new AdSearch(domainName);
      ResultPropertyValueCollection property = adSearch.GetProperty(userAccount, @"userprincipalname");
      if (property != null && property.Count == 1)
      {
          userPrincipalName = property[0].ToString();
      }
      return userPrincipalName == null
              ? WindowsIdentity.GetCurrent()
              : new WindowsIdentity(userPrincipalName);
      }