You need to sign in to do that
Don't have an account?

Using Session ID with the .NET Provider for sForce
I have been using the sForce Provider for .NET for several integrations with great success. Kudos to the developers who did a fantastic job.
I am having a problem with one particular integration in which I want to pass the session id passed from SFDC and reuse it to create a connection within my asp page. When I do this using the sForce api, it is as simple as setting the sessionheadervalue of the sForceService equal to the SessionID. Can anyone tell me how I can do the same thing with the provider?
(The reason I need to do this rather than hard-coding a user and password is that the ASPX page is going to create objects within SFDC that I would like the user to own)
Thanks.
Hi Jesip,
Well, you need to modify the sforceConnection class so that it will accept a connection string like "session id=baBxhvfYhis7Qx1;server url=https://na1-api.salesforce.com/services/Soap/u/5.0".
To do this you modify the property setter for ConnectionString to include:
If m_htbConnParms.ContainsKey("session id") Then
m_svcSforce.SessionHeaderValue = New Sforce.SessionHeader
m_svcSforce.SessionHeaderValue.sessionId = m_htbConnParms("session id").ToString
SessionID = m_htbConnParms("session id").ToString
m_authAuthType = AUTHENTICATION_TYPE.SESSIONID
End If
If m_htbConnParms.ContainsKey("server url") Then
m_svcSforce.Url = m_htbConnParms("server url").ToString
URL = m_htbConnParms("server url").ToString
m_authAuthType = AUTHENTICATION_TYPE.SESSIONID
End If
Create an Enum called AUTHENTICATION_TYPE with 2 values SESSIONID and USERNAME.
Then, in the Open method, modify the login part like below:
With m_svcSforce
m_enmConnState = ConnectionState.Connecting
If m_authAuthType = AUTHENTICATION_TYPE.USERNAME Then
Dim objLR As Sforce.LoginResult = .login(m_strUsername, m_strPassword)
m_enmConnState = ConnectionState.Open
.SessionHeaderValue = New Sforce.SessionHeader
.SessionHeaderValue.sessionId = objLR.sessionId
.Url = objLR.serverUrl
m_strUserId = objLR.userId
Else
m_enmConnState = ConnectionState.Open
m_strUserId = m_svcSforce.getUserInfo().userId
End If
If m_intQueryBatchSize > 0 Then
.QueryOptionsValue = New Sforce.QueryOptions
.QueryOptionsValue.batchSize = m_intQueryBatchSize
.QueryOptionsValue.batchSizeSpecified = True
End If
End With
This should allow you to specify a connection string that uses the session id and server url rather than the username and password.
Cheers
This is another approach that doesn't require modifying the SforceConnection class. This assumes you have two panels (panelLogin and panelRequestForm). The SessionId and URL properties are stored in ViewState via Literal controls.
private SforceConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
panelLogin.Visible = true;
}
else
{
// Handle login if login form visible; else, handle form submit
if(panelLogin.Visible)
{
string connString = "User ID=" + txtUsername.Text.Trim() + ";Password=" + txtPassword.Text.Trim();
conn = new SforceConnection(connString);
conn.URL = litSFDCEndpoint.Text;
try
{
conn.Open();
litSessionId.Text = conn.SessionID;
litSFDCEndpoint.Text = conn.URL;
panelLogin.Visible = false;
panelRequestForm.Visible = true;
}
catch(Exception err)
{
lblMessage.Text = "Error: " + err.Message + "<br><br>";
}
}
else
{
conn = new SforceConnection();
conn.SessionID = litSessionId.Text;
conn.URL = litSFDCEndpoint.Text;
try
{
// Process form (insert, update, etc)
}
catch(Exception err)
{
lblMessage.Text = "Error: " + err.Message + "<br><br>";
}
}
}
}
I took Dave's suggestion and modified ConnectionString() and Open() with a few minor alterations. Also added the enum and a couple of member vars. VB.NET 1.1 code posted below.
Code:
Along the way, however, I found that one could easily enable the existing SforceConnection object (i.e., no modifications necessary to ADO.NET provider) to assume a new SessionHeader. This is done by first logging in "traditionally" with a username and password (ideally, an API designated account), then resetting the SforceConnection's SessionHeader value to one that uses the SessionId passed in via weblink. Note that I didn't need to change the Url property, but that could be accomplished by passing the API Url via weblink, as well.
This approach is posted below in a C# 2.0 sample method below called UpdateAccount().
Code:
Glad to see you get it working!
:smileywink: