从 C# 客户端应用程序执行 Salesforce 身份验证时出错

Error while doing Salesforce Authentication from C# Client Application

下面是我编写的与 login() 一起用于 Salesforce 身份验证的完整代码。但是,如果我在 .Net 客户端(控制台应用程序)中执行代码,则会出现错误。谁能建议我如何解决这个问题。

我的现场协调员已确认,只要涉及 Salesforce,一切都很好。

代码

        private bool login()
    {
        string username = "kak.mca@gmail.com";
        string password = "Temp0r@ry";
        string securityToken = "";
        string resultSessionId = string.Empty;
        string resultServerUrl = string.Empty;
        binding = new SforceService();
        binding.Timeout = 60000;
        LoginResult lr;
        try
        {
            #region Method1
            //lr = binding.login(username, password);
            #endregion Method1
            #region Method2
            using(binding)
            {
                lr = binding.login(username, password);
                resultSessionId = lr.sessionId;
                resultServerUrl = lr.serverUrl;
            }
            #endregion Method2
            return true;
        }
        catch (SoapException e)
        {
            Console.WriteLine("Fault code: " + e.Code + Environment.NewLine + "Error message: " + e.Message + Environment.NewLine + "Stack trace:" + e.StackTrace);
            return false;
        }
    }

错误:

<font color='red'>Fault code: INVALID_LOGIN<br />
Error message: INVALID_LOGIN: Invalid username, password, security token; or user Locked out.<br />
Stack trace: at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
 at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
 at Walkthrough.sforce.SforceService.login(String username, String password) in <filepath>\Web Reference\sforce\Reference.cs:line 545
 at Walkthrough.QuickStartApiSample.login() in <filepath>\QuickstartApiSample.cs:line 54
</font>

您缺少安全令牌。您可以通过导航至 您的姓名 > 设置 > 个人设置 > 我的个人信息 > 重置我的安全令牌来获得一个。您的密码变量应该是

password = loginPassword + securityToken;

或者你可以试试Set Up Authorization, Authenticating Apps with OAuth and Digging Deeper into OAuth 2.0 on Force.com

错误是由于凭据不正确造成的。提交正确的凭据后,一切正常。