Last active
January 17, 2022 06:33
-
-
Save sandromello/813bb8502ffc9a6f194d to your computer and use it in GitHub Desktop.
Get Zimbra Token Soap API Example in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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\">[email protected]</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