esb.Url = "https://CAS01.contoso.com/EWS/exchange.asmx";
esb..CookieContainer = new CookieContainer();
If your using Raw HTML and a HttpWebRequest make sure you do the same thing before submitting the first request.
A code sample is worth a 1000 words so here's a basic Exchange Online/Office365 Autodiscover and WDSL proxy sample that just does a simple GetFolder to the Inbox.
String MbMailbox = "user@domain.onmicrosoft.com";
String AutoDiscoURL = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml";
NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com","password");
String EWSURL = null;
String auDisXML = "
"
"
"
"
System.Net.HttpWebRequest adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(AutoDiscoURL);
byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);
adAutoDiscoRequest.ContentLength = bytes.Length;
adAutoDiscoRequest.ContentType = "text/xml";
adAutoDiscoRequest.Headers.Add("Translate", "F");
adAutoDiscoRequest.Method = "POST";
adAutoDiscoRequest.Credentials = ncCred;
Stream rsRequestStream = adAutoDiscoRequest.GetRequestStream();
rsRequestStream.Write(bytes, 0, bytes.Length);
rsRequestStream.Close();
adAutoDiscoRequest.AllowAutoRedirect = false;
WebResponse adResponse = adAutoDiscoRequest.GetResponse();
String Redirect = adResponse.Headers.Get("Location");
if (Redirect != null)
{
adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(Redirect);
adAutoDiscoRequest.ContentLength = bytes.Length;
adAutoDiscoRequest.ContentType = "text/xml";
adAutoDiscoRequest.Headers.Add("Translate", "F");
adAutoDiscoRequest.Method = "POST";
adAutoDiscoRequest.Credentials = ncCred;
rsRequestStream = adAutoDiscoRequest.GetRequestStream();
}
rsRequestStream.Write(bytes, 0, bytes.Length);
rsRequestStream.Close();
adResponse = adAutoDiscoRequest.GetResponse();
Stream rsResponseStream = adResponse.GetResponseStream();
XmlDocument reResponseDoc = new XmlDocument();
reResponseDoc.Load(rsResponseStream);
XmlNodeList pfProtocolNodes = reResponseDoc.GetElementsByTagName("Protocol");
foreach (XmlNode xnNode in pfProtocolNodes)
{
XmlNodeList adChildNodes = xnNode.ChildNodes;
foreach (XmlNode xnSubChild in adChildNodes)
{
switch (xnSubChild.Name)
{
case "EwsUrl": EWSURL = xnSubChild.InnerText;
break;
}
}
}
ExchangeServiceBinding esExchangeServiceBinding = new ExchangeServiceBinding();
esExchangeServiceBinding.Credentials = ncCred;
esExchangeServiceBinding.Url = EWSURL;
esExchangeServiceBinding.CookieContainer = new CookieContainer();
DistinguishedFolderIdType[] dsFolderid = new DistinguishedFolderIdType[1];
dsFolderid[0] = new DistinguishedFolderIdType();
dsFolderid[0].Id = DistinguishedFolderIdNameType.inbox;
GetFolderType gfFolderType = new GetFolderType();
gfFolderType.FolderIds = dsFolderid;
gfFolderType.FolderShape = new FolderResponseShapeType();
gfFolderType.FolderShape.BaseShape = DefaultShapeNamesType.Default;
GetFolderResponseType gfGetFolderResponse = esExchangeServiceBinding.GetFolder(gfFolderType);
if (gfGetFolderResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success) {
FolderType Folder = (FolderType)((FolderInfoResponseMessageType)gfGetFolderResponse.ResponseMessages.Items[0]).Folders[0];
Console.WriteLine(Folder.DisplayName);
}
Eventhough after setting cookie it is returning 401.
ReplyDelete