Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created May 31, 2016 07:41
Show Gist options
  • Save ahelland/57f48a5bbf5c65bae5d015467de4d109 to your computer and use it in GitHub Desktop.
Save ahelland/57f48a5bbf5c65bae5d015467de4d109 to your computer and use it in GitHub Desktop.
TimerTriggered Function for monitoring Exchange ActiveSync
#r "EAS Protocol.dll"
#r "System.Xml.Linq.dll"
using System;
using System.Net.Http.Headers;
using System.Xml.Linq;
using EAS_Protocol.WBXML;
public static void Run(TimerInfo myTimer,out string statuscode, out string responseBlob, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
string strServerAddress = "https://outlook.office365.com/Microsoft-Server-ActiveSync";
string strDeviceId = "EASMD";
string strDeviceType = "AzureFunction";
string strUserAgent = "AzureFunction";
string strASVersion = "14.1";
string strUsername = "user@domain.com";
string strPassword = "p@ssw0rd";
string strEncCredentials = getEncCredentials(strUsername, strPassword);
string uri = strServerAddress + "?Cmd=FolderSync&User=" + strUsername + "&DeviceId=" + strDeviceId +"&DeviceType=" + strDeviceType;
log.Info($"Uri: {uri}");
//We need a custom handler as we don't want to automatically follow 302s
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
var responseString = "";
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.ms-sync.wbxml"));
client.DefaultRequestHeaders.Add("MS-ASProtocolVersion", strASVersion);
client.DefaultRequestHeaders.Add("User-Agent", strUserAgent);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", strEncCredentials);
//The wbxml required for initial FolderSync
byte[] postBytes = new byte[13]{03,01,106,00,00,07,86,82,03,48,00,01,01};
HttpContent byteContent = new ByteArrayContent(postBytes);
HttpResponseMessage response = client.PostAsync(uri,byteContent).Result;
responseString = response.Content.ReadAsStringAsync().Result;
var status = (int)response.StatusCode;
var guid = Guid.NewGuid();
//Output status code to queue
statuscode = $"{guid};{status}";
responseBlob = $"{guid};{responseString}";
System.Text.UTF8Encoding utf8Encoding = new System.Text.UTF8Encoding();
Byte[] byteResp = utf8Encoding.GetBytes(responseString);
log.Info($"Http: {status}, Content: {responseString}");
WBXMLWriter wbxmlWriter = new WBXMLWriter(new ASCodePageProvider());
WBXMLConverter wbxmlConverter = new WBXMLConverter(new ASCodePageProvider(), wbxmlWriter, null);
XElement destXml = wbxmlConverter.Parse(byteResp);
log.Info($"WBXML:{System.Environment.NewLine}{destXml.ToString()}");
}
}
private static string getEncCredentials(string emailadress, string password)
{
string strEmailAddress = emailadress;
string strPassword = password;
string strRawCredentials = strEmailAddress + ":" + strPassword;
//Need to base64 encode the credentials
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteSource = encoding.GetBytes(strRawCredentials);
string strEncCredentials = System.Convert.ToBase64String(byteSource);
return strEncCredentials;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment