Logging In
Overview
A C# example demonstrating how to log in and retrieve a session key using SOAP and the v4 SOAP API.
Example
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().ToLower(); //lowercase as of 7.9.0.0
}
}
}
Note: As of 7.9.0.0, the md5 of the password must be lowercase.
Result
b2uv0vrjfiov41d03sk578ufq6