Skip to content

Instantly share code, notes, and snippets.

@sugarknowledge
Created June 27, 2012 04:51
Show Gist options
  • Save sugarknowledge/3001549 to your computer and use it in GitHub Desktop.
Save sugarknowledge/3001549 to your computer and use it in GitHub Desktop.
C# Example using SOAP to retrieve a session id
b2uv0vrjfiov41d03sk578ufq6
using System;
using System.Text;
using System.Security.Cryptography;
namespace SugarSoap
{
class Program
{
static void Main(string[] args)
{
string UserName = "admin";
string Password = "password";
string URL = "http://{site_url}/service/v4/soap.php";
string SessionID = String.Empty;
//SugarCRM is a web reference added that points to http://{site_url}/service/v4/soap.php?wsdl
SugarCRM.sugarsoap SugarClient = new SugarCRM.sugarsoap();
SugarClient.Timeout = 900000;
SugarClient.Url = URL;
//Create authentication object
SugarCRM.user_auth UserAuth = new SugarCRM.user_auth();
//Populate credentials
UserAuth.user_name = UserName;
UserAuth.password = getMD5(Password);
//Try to authenticate
SugarCRM.name_value[] LoginList = new SugarCRM.name_value[0];
SugarCRM.entry_value LoginResult = SugarClient.login(UserAuth, "SoapTest", LoginList);
//get session id
SessionID = LoginResult.id;
if (SessionID != String.Empty)
{
//print session
Console.WriteLine(SessionID);
}
//Pause Window
Console.ReadLine();
}
static private string getMD5(string TextString)
{
MD5 md5 = MD5.Create();
byte[] inputBuffer = System.Text.Encoding.ASCII.GetBytes(TextString);
byte[] outputBuffer = md5.ComputeHash(inputBuffer);
StringBuilder Builder = new StringBuilder(outputBuffer.Length);
for (int i = 0; i < outputBuffer.Length; i++)
{
Builder.Append(outputBuffer[i].ToString("X2"));
}
return Builder.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment