Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用访问令牌在C#中向Dynamics CRM发布数据_C#_Asp.net Mvc_Post_Dynamics Crm_Crm - Fatal编程技术网

如何使用访问令牌在C#中向Dynamics CRM发布数据

如何使用访问令牌在C#中向Dynamics CRM发布数据,c#,asp.net-mvc,post,dynamics-crm,crm,C#,Asp.net Mvc,Post,Dynamics Crm,Crm,我需要将MVC表单中的数据发布到我们的Dynamics CRM,我有一个获取访问令牌的函数设置,但我不知道如何使用它将数据发送到CRM。我可以通过邮递员在CRM中发布和创建记录,但我不知道如何将访问令牌和邮递员在C#中发布结合在一起 获取访问令牌: string organizationUrl = "https://myorgcrm.crm4.dynamics.com"; string appKey = "XXXXXXxxxXxXXXxXXXXXX+XXxxxxXXxxxXXxxxXXXXX="

我需要将MVC表单中的数据发布到我们的Dynamics CRM,我有一个获取访问令牌的函数设置,但我不知道如何使用它将数据发送到CRM。我可以通过邮递员在CRM中发布和创建记录,但我不知道如何将访问令牌和邮递员在C#中发布结合在一起

获取访问令牌:

string organizationUrl = "https://myorgcrm.crm4.dynamics.com";
string appKey = "XXXXXXxxxXxXXXxXXXXXX+XXxxxxXXxxxXXxxxXXXXX=";
string aadInstance = "https://login.microsoftonline.com/";
string tenantID = "myorg.onmicrosoft.com";
string clientId = "XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";   

public string AuthenticateWithCRM()
{ 
    AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);

    ClientCredential clientcred = new ClientCredential(clientId, appKey);
    AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID);
    AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(organizationUrl, clientcred).Result;

    return authenticationResult.AccessToken;
}
这将返回一个访问令牌,因为它返回一个令牌,我假设这是正确的,但在我知道如何尝试发送数据之前,我无法确定

邮递员

JSON:


我使用一个访问令牌来运行它,该令牌填写身份验证URL、访问令牌URL和客户端ID,然后根据我的工作windows用户帐户工作

不太熟悉Dynamics CRM,但从我看来,它似乎很熟悉

通常,当用户登录到您的应用程序时,或者当您的应用程序启动时,您会请求现有的accessToken。根据OAuth,您必须在任何请求中将accesstoken作为头传递。用C#做这件事

{
    "org_accountnumber": '12345',
    "org_Individualid":
    {
        "firstname": "Luke",
        "lastname": "Skywalker"
    },
    "orgstuff@odata.bind":"/orgstuff_schemes(XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX)"
}
using (HttpClient httpClient = new HttpClient())
{
    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes
    httpClient.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue("Bearer", result.AccessToken);