簡體   English   中英

如何使用訪問令牌將數據發布到C#中的Dynamics CRM中

[英]How to post data to Dynamics CRM in C# using Access Token

我需要將數據從MVC表單發布到我們的Dynamics CRM,我已經獲得了獲取訪問令牌的功能設置,但是我不知道如何使用它來將數據發送到CRM。 我可以通過Postman在CRM中發布和創建記錄,但是我不知道如何在C#中將Access Token和Postman發布結合在一起。

獲取訪問令牌:

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:

{
    "org_accountnumber": '12345',
    "org_Individualid":
    {
        "firstname": "Luke",
        "lastname": "Skywalker"
    },
    "orgstuff@odata.bind":"/orgstuff_schemes(XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX)"
}

我使用在身份驗證URL,訪問令牌URL和客戶端ID中填寫的訪問令牌運行此文件,然后它根據我的工作Windows用戶帳戶運行。

Dynamics CRM並不熟悉,但是從MS DOCS上發現它似乎很熟悉。

通常,當用戶登錄到您的應用程序或應用程序引導時,您通常會請求accessToken。 根據OAuth,您必須在任何請求中將accesstoken作為標頭傳遞。 在C#中做到這一點

using (HttpClient httpClient = new HttpClient())
{
    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes
    httpClient.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue("Bearer", result.AccessToken);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM