1
votes

I am following this tutorial http://wiki.developerforce.com/page/Integrating_Force.com_with_Microsoft_.NET

However, I am getting this error:

LOGIN_MUST_USE_SECURITY_TOKEN: Invalid username, password, security token; or user locked out. Are you at a new location? When accessing Salesforce--either via a desktop client or the API--from outside of your company’s trusted networks, you must add a security token to your password to log in. To receive a new security token, log in to salesforce.com at http://login.salesforce.com and click Setup | My Personal Information | Reset Security Token.

This is my code in my Console App:

static void Main(string[] args)
        {
            string userName;
            string password;
            userName = "[email protected]";
            password = "myPassword";

            SforceService SfdcBinding = null;
            LoginResult CurrentLoginResult = null;
            SfdcBinding = new SforceService();
            try
            {
                CurrentLoginResult = SfdcBinding.login(userName, password);
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {
                // This is likley to be caused by bad username or password
                SfdcBinding = null;
                throw (e);
            }
            catch (Exception e)
            {
                // This is something else, probably comminication
                SfdcBinding = null;
                throw (e);
            }

        }

The error states I need a security token, but the documentation never seems to mention it and I'm not sure how to get one.

2
Sorry to bother you. I am following the same tutorial. I have added the wsdl xml file as instructed and added the service reference. Then in my code I have exactly as you have above but and added using BST.SFDC;But on the line SforceService SfdcBinding = null; I am getting the error "The type or namespace name 'SforceService' could not be found (are you missing a using directive or an assembly reference?)" - Trevor Daniel
@TrevorDaniel I believe you have to add a using [WhateverNameYouGaveYourWSDLFile]; statement. I can double check for you when I get in the office tomorrow; but I am fairly certain. Let me know if it works for you. - user1477388
im wondering if I have used the wrong wsdl file - im using the enterprise one? which one did you use? - Trevor Daniel
@TrevorDaniel Fairly certain I used the Partner WSDL - user1477388

2 Answers

9
votes

What I had to do (which was not in the documentation) is go to here:

https://na15.salesforce.com/_ui/system/security/ResetApiTokenConfirm?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DPersonalInfo&setupid=ResetApiToken

And, reset my token. Then, append it to the end of my password like:

If your password = "mypassword"

And your security token = "XXXXXXXXXX"

You must enter "mypasswordXXXXXXXXXX" in place of your password

Ref. http://docs.servicerocket.com/pages/viewpage.action?pageId=83099770

2
votes

With a SOAP API like this you need to authenticate to the service first by providing the username and password. Their response should return an authorization token that will be valid for a period of time. Then in your subsequent communications you pass this token to the API so that it knows who you are.

Get the authorization token:

SforceService SfdcBinding = null;
LoginResult CurrentLoginResult = null;
SfdcBinding = new SforceService();
try 
{
   CurrentLoginResult = SfdcBinding.login(userName, password);
}
catch (System.Web.Services.Protocols.SoapException e) 
{
   // This is likely to be caused by bad username or password
   SfdcBinding = null;
   throw (e);
}
catch (Exception e) 
{
   // This is something else, probably communication
   SfdcBinding = null;
   throw (e);
}

Setup the session:

//Change the binding to the new endpoint
SfdcBinding.Url = CurrentLoginResult.serverUrl;

//Create a new session header object and set the session id to that returned by the login
SfdcBinding.SessionHeaderValue = new SessionHeader();
SfdcBinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;

Perform your query:

QueryResult queryResult = null;
String SOQL = "select FirstName, LastName, Phone from Lead where email = '[email protected]'";
queryResult = SfdcBinding.query(SOQL);