Question

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 = "me@myWebsite.com";
            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.

Was it helpful?

Solution

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

OTHER TIPS

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 = 'john.smith@salesforce.com'";
queryResult = SfdcBinding.query(SOQL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top