Skip to content

Instantly share code, notes, and snippets.

@sandromello
Last active January 17, 2022 06:33
Show Gist options
  • Save sandromello/813bb8502ffc9a6f194d to your computer and use it in GitHub Desktop.
Save sandromello/813bb8502ffc9a6f194d to your computer and use it in GitHub Desktop.
Get Zimbra Token Soap API Example in C#
// Tested with version 8.0.6
// Read more at http://wiki.zimbra.com/wiki/SOAP_API_Reference_Material_Beginning_with_ZCS_8.0#ZCS_8.0.6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication1
{
class Program : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint,
X509Certificate certificate, WebRequest request,
int certificateProblem)
{
//Return True to force the certificate to be accepted.
return true;
}
static void Main(string[] args)
{
System.Net.ServicePointManager.CertificatePolicy = new Program();
StringBuilder xml = new StringBuilder();
xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
xml.Append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">");
xml.Append("<soap:Header>");
xml.Append("<context xmlns=\"urn:zimbra\">");
xml.Append("<format type=\"xml\"/>");
xml.Append("</context>");
xml.Append("</soap:Header>");
xml.Append("<soap:Body>");
xml.Append("<AuthRequest xmlns=\"urn:zimbraAdmin\">");
xml.Append("<account by=\"name\">user@domain.tld</account>");
xml.Append("<password>admin_secure_password</password>");
xml.Append("</AuthRequest>");
xml.Append("</soap:Body>");
xml.Append("</soap:Envelope>");
Console.WriteLine(xml);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://domain.tld:7071/service/admin/soap");
httpRequest.ContentType = "application/soap+xml";
byte[] byteArray = Encoding.UTF8.GetBytes(xml.ToString());
httpRequest.Method = "POST";
httpRequest.ContentLength = byteArray.Length;
using (var stream = httpRequest.GetRequestStream())
{
stream.Write(byteArray, 0, byteArray.Length);
}
var response = (HttpWebResponse)httpRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment