来自C#客户端的Salesforce身份验证出错 [英] Error with SalesForce authentication from C# Client(s)

查看:31
本文介绍了来自C#客户端的Salesforce身份验证出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Salesforce的新手,正在努力掌握基础知识,因为我们下周将开始一个大型集成项目。至少对于我们集成的一部分,我们将需要访问Salesforce睡觉API。为此,我和团队中的另一位先生使用Postman成功地对调用进行了一些基本的原型设计,特别是在OAuth2身份验证方面。我们的问题是,虽然Postman看起来一切正常,但是一旦我们切换到使用C#进行调用,我们就开始收到错误,而不是我们的auth令牌。响应为HTTP Status 400 Bad Request,响应内容为

{"error":"invalid_grant","error_description":"authentication failure"}.

以下是我们尝试过的一些C#代码示例:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;

namespace SFTokenTest
{
    internal class Program
    {
        private const string LoginUrl = "https://test.salesforce.com/services/oauth2/token";

        private static void Main(string[] args)
        {
            FormUrlEncodedContent content = new FormUrlEncodedContent(new []
            {
            new KeyValuePair<string, string>("grant_type",HttpUtility.UrlEncode("password")),
            new KeyValuePair<string, string>("password",HttpUtility.UrlEncode("PASSWORD")),
            new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("USERNAME")),
            new KeyValuePair<string, string>("client_id",HttpUtility.UrlEncode("CLIENTID")),
            new KeyValuePair<string, string>("client_secret",HttpUtility.UrlEncode("CLIENTSECRET"))
            });
            HttpResponseMessage response;
            using (HttpClient client = new HttpClient())
            {
                response = client.PostAsync(LoginUrl, content).Result;
            }

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            Console.WriteLine(response.StatusCode);

            Console.ReadLine();
        }
    }
}

注意:这是我们尝试过的众多实现之一,包括使用HttpWebRequest和WebClient,结果相同。

除了我们自己的代码之外,我们还尝试从GitHub存储库中提取代码developerforce/Force.com-Toolkit-for-Net,这也出现了同样的问题。

到目前为止,我们已经对此进行了两天的故障排除,但一无所获,我们已经难倒了我们的Salesforce顾问,告诉他们它为什么不能工作。我们今天下午也尝试了重置安全令牌,但也无济于事。我在网上浏览了很多文章(包括StackOverflow上的文章),其中大部分都指向以下问题:

  • 凭据错误-我们在Postman中成功使用了与我们尝试在C#应用程序中使用的凭据相同的凭据,并且我们已尝试从Salesforce UI重新复制和粘贴它们,并尝试将它们手动键入我们的代码中,以防我们拾取不可见的字符。(通过凭据,我包括用户名、密码、client_id和客户端密码。)
  • Content-Type标头错误或丢失-我们肯定是在发送"application/x-www-form-urlencode",我已经在每个应用程序中检查了很多次(并且已经嗅探以验证它是否真的在发送)。
  • 未编码的参数-我在许多StackOverflow线程上看到了这一点,并已验证我们正在对我们在请求正文中发送的参数进行编码。

似乎Postman正在做一些我们的请求中没有的事情,但我似乎不能准确地指出是什么。我们甚至捕获了来自Postman和我们的一个客户的请求,并对它们进行了区分,返回的结果是完全相同的。

如有任何帮助,我们将不胜感激。

推荐答案

如果您尚未解决此问题,我认为这可能是您的问题。 那么根据这个:https://help.salesforce.com/apex/HTViewSolution?id=000221207 从2016年6月开始,Salesforce将分阶段在其整个平台上禁用TLS 1.0加密。

如果您使用的是.NET4.6之前的任何版本,这意味着默认情况下不会打开TLS1.1/1,2。这里有一个您可以用来启用它的选项列表:https://help.salesforce.com/apex/HTViewSolution?id=000221207#Inboundintegrations

对于像您这样的控制台应用程序,刚刚在我这边进行了测试,以下内容对我有效(在.Net 4.5.2上):

        string LoginUrl = "https://login.salesforce.com/services/oauth2/token";
        //the line below enables TLS1.1 and TLS1.2
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
        FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
        {
        new KeyValuePair<string, string>("grant_type", "password"),
        new KeyValuePair<string, string>("client_id","CLIENTID"),
        new KeyValuePair<string, string>("client_secret","CLIENTSECRET"),
        new KeyValuePair<string, string>("password","PASSWORD + SECURITYTOKEN"),
        new KeyValuePair<string, string>("username", "USERNAME")
        });
        HttpResponseMessage response;
        using (HttpClient client = new HttpClient())
        {
            response = client.PostAsync(LoginUrl, content).Result;
        }
        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
        Console.WriteLine(response.StatusCode);
        Console.ReadLine();

这篇关于来自C#客户端的Salesforce身份验证出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆