Skip to content

Instantly share code, notes, and snippets.

@devishot
Created December 15, 2016 20:41
Show Gist options
  • Save devishot/ad3db1b7b41b11c3a02c24245e05d03f to your computer and use it in GitHub Desktop.
Save devishot/ad3db1b7b41b11c3a02c24245e05d03f to your computer and use it in GitHub Desktop.
Synergy + AD + Exchange APIs mix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
using System.Web;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net.Cache;
using System.Web.Script.Serialization;
using System.IO;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
namespace testExchangeAPI
{
[DataContract]
class CreateTaskResponse
{
[DataMember(Name = "errorCode")]
public int ErrorCode { get; set; }
[DataMember(Name = "errorMessage")]
public string ErrorMessage { get; set; }
[DataMember(Name = "taskID", EmitDefaultValue = false, IsRequired = false)]
public string TaskID { get; set; }
}
public class SynergyUserIDNotFoundException : System.Exception
{
public SynergyUserIDNotFoundException()
: base()
{
}
public SynergyUserIDNotFoundException(string message)
: base(message)
{
}
public SynergyUserIDNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
class Program
{
private static ExchangeService service;
private static String ARTA_HOST = "http://176.119.231.62:8080/Synergy/";
private static String AD_DOMAIN = "Arta";
private static String USER_ADMIN_ID = "1";
private static String USER_ADMIN_EMAIL = "[email protected]"; //"[email protected]";
private static String USER_ADMIN_PASSWORD = "B3413df1"; //"B3413df1";
private static String FORM_USER_ADDITIONAL = "c8c346c4-d637-4b81-adae-8f26e8dd2062";
private static String HTTP_HEADER_AUTHORIZATION = "Basic YWRtaW5pc3RyYXRvcjphZG1pbmlzdHJhdG9y";
private static String HTTP_HEADER_ACCEPT = "application/json;charset=utf-8";
static void Main(string[] args)
{
DissableSSLCheck();
InitService();
// 1.a get email
// 1.b get AD login
//FindItemsResults<Appointment> appointments = GetCalendarAppointments("[email protected]");
string adUserID = "ArtaAdmin";
string adUserEmail = "[email protected]";
try
{
var appointments = FindCalendarAppointments(adUserEmail);
var userID = FindSynergyUserID(adUserID);
Console.Write("userID is " + userID);
var adUserLogins = FindADUserLogins();
Console.WriteLine("AD Users: count={0}", adUserLogins.Count);
foreach (var login in adUserLogins)
{
Console.WriteLine("- " + login);
}
var adUserEmails = FindADUserEmails(adUserLogins);
Console.WriteLine("AD User emails: count={0}", adUserEmails.Count);
foreach (var email in adUserEmails)
{
Console.WriteLine("* " + email);
}
//var tasks = FindSynergyTasks(userID);
}
catch (SynergyUserIDNotFoundException ex)
{
Console.WriteLine(".SynergyUserID.notFound. for: ", adUserID);
}
/*
foreach (Appointment a in appointments)
{
CreateTask()
}
*/
}
public static List<String> FindADUserEmails(List<String> adUserLogins)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + AD_DOMAIN);
DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(objectClass=user)";
List<String> userEmails = new List<String>();
foreach (SearchResult resultSet in dSearch.FindAll())
{
if (resultSet.Properties["mail"].Count > 0)
{
string email = resultSet.Properties["mail"][0].ToString();
userEmails.Add(email);
}
}
return userEmails;
}
public static List<String> FindADUserLogins()
{
using (var context = new PrincipalContext(ContextType.Domain, AD_DOMAIN))
{
using (var searched = new PrincipalSearcher(new UserPrincipal(context)))
{
List<String> logins = new List<String>();
foreach (var result in searched.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
// pre-windows 2000 - your Windows/AD account name
var samAccountName = de.Properties["samAccountName"].Value;
// after windows 200 - your "[email protected]" style name
// var userPrincipalName = de.Properties["userPrincipalName"].Value;
logins.Add(samAccountName.ToString());
}
return logins;
}
}
}
public static String FindSynergyTasks(string userID)
{
// TODO
return null;
}
public static String FindSynergyUserID(string ad_login)
{
string urlAddress = ARTA_HOST + "rest/api/filecabinet/get_by_field_value";
var uriBuilder = new UriBuilder(urlAddress);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["formUUID"] = FORM_USER_ADDITIONAL;
query["fieldName"] = "login_ad";
query["value"] = ad_login;
uriBuilder.Query = query.ToString();
string url = uriBuilder.ToString();
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Accept = HTTP_HEADER_ACCEPT;
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.Headers.Add(HttpRequestHeader.Authorization, HTTP_HEADER_AUTHORIZATION);
HttpWebResponse response = null;
try
{
response = request.GetResponse() as HttpWebResponse;
var reader = new StreamReader(response.GetResponseStream());
JavaScriptSerializer js = new JavaScriptSerializer();
var userIDs = js.Deserialize<List<String>>(reader.ReadToEnd());
if (userIDs.Count == 0)
{
throw new SynergyUserIDNotFoundException();
}
else
{
return userIDs.ElementAt(0);
}
}
catch (WebException e)
{
response = (HttpWebResponse)e.Response;
StreamReader sr = new StreamReader(response.GetResponseStream());
Console.WriteLine("- ErrorCode: {0} ", (int)response.StatusCode);
Console.WriteLine("- Body: {0}", sr.ReadToEnd());
return null;
}
finally
{
if (response != null)
{
response.Close();
}
}
}
// MARK: - ARTA Synergy API
//Create the request URL
public static void CreateTask(string userID, string description, string date)
{
try
{
string urlAddress = ARTA_HOST + "rest/api/diary/tasks/" + USER_ADMIN_ID + "/save";
HttpWebRequest request = WebRequest.Create(new Uri(urlAddress)) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// Create request Body
Dictionary<string, string> bodyParams = new Dictionary<string, string>
{
{"userID", userID},
{"task", description},
{"wholeDay", "false"},
{"start", date},
{"finish", date}
};
StringBuilder bodyFormEncodedParams = new StringBuilder();
foreach (var pair in bodyParams)
{
bodyFormEncodedParams.Append(pair.Key);
bodyFormEncodedParams.Append("=");
bodyFormEncodedParams.Append(HttpUtility.UrlEncode(pair.Value));
bodyFormEncodedParams.Append("&");
}
// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(bodyFormEncodedParams.ToString());
request.ContentLength = formData.Length;
// Send the request:
using (System.IO.Stream post = request.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
// Check response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(CreateTaskResponse));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
CreateTaskResponse createTaskResponse = objResponse as CreateTaskResponse;
if (createTaskResponse.ErrorCode != 0)
{
Console.WriteLine("SynergyAPI.createTask.error: ", createTaskResponse.ErrorCode, createTaskResponse.ErrorMessage, bodyFormEncodedParams);
return;
}
Console.WriteLine("SynergyAPI.done: ", createTaskResponse.TaskID);
return;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
}
// MARK: - MS Exchange API
private static void DissableSSLCheck()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
}
private static void InitService()
{
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.Credentials = new WebCredentials(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD, "arta.pech.kz");
//service.UseDefaultCredentials = true;
service.AutodiscoverUrl(USER_ADMIN_EMAIL, RedirectionUrlValidationCallback);
}
private static void TestSendEmail()
{
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("[email protected]");
email.ToRecipients.Add("[email protected]");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API.");
email.Send();
}
private static FindItemsResults<Appointment> FindCalendarAppointments(string userEmail)
{
// Initialize values for the start and end times, and the number of appointments to retrieve.
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(30);
const int NUM_APPTS = 5;
// Initialize the calendar folder object with only the folder ID.
CalendarFolder calendar = CalendarFolder.Bind(service, new FolderId(WellKnownFolderName.Calendar, userEmail), new PropertySet());
// Set the start and end time and number of appointments to retrieve.
CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
// Limit the properties returned to the appointment's subject, start time, and end time.
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
// Retrieve a collection of appointments by using the calendar view.
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
" to " + endDate.Date.ToShortDateString() + " are: \n");
foreach (Appointment a in appointments)
{
Console.Write("Subject: " + a.Subject.ToString() + " ");
Console.Write("Start: " + a.Start.ToString() + " ");
Console.Write("End: " + a.End.ToString());
Console.WriteLine();
}
return appointments;
}
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation
// callback, the redirection URL is considered valid if it is using HTTPS
// to encrypt the authentication credentials.
if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment