Created
February 20, 2014 01:34
-
-
Save wadewegner/9105326 to your computer and use it in GitHub Desktop.
Async login against either the Partner or Enterprise SOAP API
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
| static async Task<T> Login<T>(string userName, string password, string orgId) | |
| { | |
| string url; | |
| string soap; | |
| string wsdlType; | |
| if (typeof(T) == typeof(Enterprise.LoginResult)) | |
| { | |
| url = "https://login.salesforce.com/services/Soap/c/29.0/" + orgId; | |
| soap = string.Format(@"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><login xmlns=""urn:enterprise.soap.sforce.com""><username>{0}</username><password>{1}</password></login></s:Body></s:Envelope>", userName, password); | |
| wsdlType = "enterprise"; | |
| } | |
| else | |
| { | |
| url = "https://login.salesforce.com/services/Soap/u/29.0/" + orgId; | |
| soap = string.Format(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:partner.soap.sforce.com""><soapenv:Body><urn:login><urn:username>{0}</urn:username><urn:password>{1}</urn:password></urn:login></soapenv:Body></soapenv:Envelope>", userName, password); | |
| wsdlType = "partner"; | |
| } | |
| var content = new StringContent(soap, Encoding.UTF8, "text/xml"); | |
| using (var httpClient = new HttpClient()) | |
| { | |
| var request = new HttpRequestMessage(); | |
| request.RequestUri = new Uri(url); | |
| request.Method = HttpMethod.Post; | |
| request.Content = content; | |
| request.Headers.Add("SOAPAction", "login"); | |
| var responseMessage = await httpClient.SendAsync(request); | |
| var response = await responseMessage.Content.ReadAsStringAsync(); | |
| if (responseMessage.IsSuccessStatusCode) | |
| { | |
| var resultXml = new XmlDocument(); | |
| var mgr = new XmlNamespaceManager(resultXml.NameTable); | |
| mgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); | |
| mgr.AddNamespace("ns", string.Format("urn:{0}.soap.sforce.com", wsdlType)); | |
| resultXml.LoadXml(response); | |
| var loginResultNode = resultXml.SelectSingleNode("//ns:loginResponse", mgr).InnerXml; | |
| var serializer = new XmlSerializer(typeof(T)); | |
| var stringReader = new StringReader(loginResultNode); | |
| var loginResult = (T)serializer.Deserialize(stringReader); | |
| stringReader.Dispose(); | |
| return loginResult; | |
| } | |
| throw new Exception("Failed login"); | |
| } | |
| } |
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
| using System.Xml.Serialization; | |
| namespace Tester.Models.Enterprise | |
| { | |
| [XmlRoot(Namespace = "urn:enterprise.soap.sforce.com", ElementName = "result", IsNullable = true)] | |
| public class LoginResult | |
| { | |
| [XmlElement(ElementName = "metadataServerUrl")] | |
| public string MetadataServerUrl; | |
| [XmlElement(ElementName = "passwordExpired")] | |
| public bool PasswordExpired; | |
| [XmlElement(ElementName = "sandbox")] | |
| public bool Sandbox; | |
| [XmlElement(ElementName = "serverUrl")] | |
| public string ServerUrl; | |
| [XmlElement(ElementName = "sessionId")] | |
| public string SessionId; | |
| [XmlElement(ElementName = "userId")] | |
| public string UserId; | |
| [XmlElement(ElementName = "userInfo")] | |
| public UserInfoResult UserInfo; | |
| } | |
| } | |
| namespace Tester.Models.Partner | |
| { | |
| [XmlRoot(Namespace = "urn:partner.soap.sforce.com", ElementName = "result", IsNullable = true)] | |
| public class LoginResult | |
| { | |
| [XmlElement(ElementName = "metadataServerUrl")] | |
| public string MetadataServerUrl; | |
| [XmlElement(ElementName = "passwordExpired")] | |
| public bool PasswordExpired; | |
| [XmlElement(ElementName = "sandbox")] | |
| public bool Sandbox; | |
| [XmlElement(ElementName = "serverUrl")] | |
| public string ServerUrl; | |
| [XmlElement(ElementName = "sessionId")] | |
| public string SessionId; | |
| [XmlElement(ElementName = "userId")] | |
| public string UserId; | |
| [XmlElement(ElementName = "userInfo")] | |
| public UserInfoResult UserInfo; | |
| } | |
| } |
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
| var loginResult = await Login<Enterprise.LoginResult>(userName, password, orgId); | |
| ... or ... | |
| var loginResult = await Login<Partner.LoginResult>(userName, password, orgId); |
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
| using System.Xml.Serialization; | |
| namespace Tester.Models.Enterprise | |
| { | |
| [XmlRoot(Namespace = "urn:enterprise.soap.sforce.com", ElementName = "userInfo", IsNullable = true)] | |
| public class UserInfoResult | |
| { | |
| [XmlElement(ElementName = "accessibilityMode")] | |
| public bool AccessibilityMode; | |
| [XmlElement(ElementName = "currencySymbol")] | |
| public string CurrencySymbol; | |
| [XmlElement(ElementName = "orgAttachmentFileSizeLimit")] | |
| public int OrgAttachmentFileSizeLimit; | |
| [XmlElement(ElementName = "orgDefaultCurrencyIsoCode")] | |
| public string OrgDefaultCurrencyIsoCode; | |
| [XmlElement(ElementName = "orgDisallowHtmlAttachments")] | |
| public bool OrgDisallowHtmlAttachments; | |
| [XmlElement(ElementName = "orgHasPersonAccounts")] | |
| public bool OrgHasPersonAccounts; | |
| [XmlElement(ElementName = "organizationId")] | |
| public string OrganizationId; | |
| [XmlElement(ElementName = "organizationMultiCurrency")] | |
| public bool OrganizationMultiCurrency; | |
| [XmlElement(ElementName = "organizationName")] | |
| public string OrganizationName; | |
| [XmlElement(ElementName = "profileId")] | |
| public string ProfileId; | |
| [XmlElement(ElementName = "roleId")] | |
| public string RoleId; | |
| [XmlElement(ElementName = "sessionSecondsValid")] | |
| public int SessionSecondsValid; | |
| [XmlElement(ElementName = "userDefaultCurrencyIsoCode")] | |
| public string UserDefaultCurrencyIsoCode; | |
| [XmlElement(ElementName = "userEmail")] | |
| public string UserEmail; | |
| [XmlElement(ElementName = "userFullName")] | |
| public string UserFullName; | |
| [XmlElement(ElementName = "userId")] | |
| public string UserId; | |
| [XmlElement(ElementName = "userLanguage")] | |
| public string UserLanguage; | |
| [XmlElement(ElementName = "userLocale")] | |
| public string UserLocale; | |
| [XmlElement(ElementName = "userName")] | |
| public string UserName; | |
| [XmlElement(ElementName = "userTimeZone")] | |
| public string UserTimeZone; | |
| [XmlElement(ElementName = "userType")] | |
| public string UserType; | |
| [XmlElement(ElementName = "userUiSkin")] | |
| public string UserUiSkin; | |
| } | |
| } | |
| namespace Tester.Models.Partner | |
| { | |
| [XmlRoot(Namespace = "urn:partner.soap.sforce.com", ElementName = "userInfo", IsNullable = true)] | |
| public class UserInfoResult | |
| { | |
| [XmlElement(ElementName = "accessibilityMode")] | |
| public bool AccessibilityMode; | |
| [XmlElement(ElementName = "currencySymbol")] | |
| public string CurrencySymbol; | |
| [XmlElement(ElementName = "orgAttachmentFileSizeLimit")] | |
| public int OrgAttachmentFileSizeLimit; | |
| [XmlElement(ElementName = "orgDefaultCurrencyIsoCode")] | |
| public string OrgDefaultCurrencyIsoCode; | |
| [XmlElement(ElementName = "orgDisallowHtmlAttachments")] | |
| public bool OrgDisallowHtmlAttachments; | |
| [XmlElement(ElementName = "orgHasPersonAccounts")] | |
| public bool OrgHasPersonAccounts; | |
| [XmlElement(ElementName = "organizationId")] | |
| public string OrganizationId; | |
| [XmlElement(ElementName = "organizationMultiCurrency")] | |
| public bool OrganizationMultiCurrency; | |
| [XmlElement(ElementName = "organizationName")] | |
| public string OrganizationName; | |
| [XmlElement(ElementName = "profileId")] | |
| public string ProfileId; | |
| [XmlElement(ElementName = "roleId")] | |
| public string RoleId; | |
| [XmlElement(ElementName = "sessionSecondsValid")] | |
| public int SessionSecondsValid; | |
| [XmlElement(ElementName = "userDefaultCurrencyIsoCode")] | |
| public string UserDefaultCurrencyIsoCode; | |
| [XmlElement(ElementName = "userEmail")] | |
| public string UserEmail; | |
| [XmlElement(ElementName = "userFullName")] | |
| public string UserFullName; | |
| [XmlElement(ElementName = "userId")] | |
| public string UserId; | |
| [XmlElement(ElementName = "userLanguage")] | |
| public string UserLanguage; | |
| [XmlElement(ElementName = "userLocale")] | |
| public string UserLocale; | |
| [XmlElement(ElementName = "userName")] | |
| public string UserName; | |
| [XmlElement(ElementName = "userTimeZone")] | |
| public string UserTimeZone; | |
| [XmlElement(ElementName = "userType")] | |
| public string UserType; | |
| [XmlElement(ElementName = "userUiSkin")] | |
| public string UserUiSkin; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment