Dynamics CRM 2015/2016/365 Web API:用户模拟

今天再更新一篇关于在Web API接口中使用用户模拟这一功能,用户模拟在Dynamics CRM开发中已经不是新概念了,历任接口都支持这个功能。在介绍怎么在Web API中使用该功能之前,我们先来过一遍用户模拟的具体功能。

用户模拟,顾名思义,即为模拟其他用户。在调用接口的时候如果我们都需要提供一个账号密码信息,但是很多时候,我们的接口的认证信息是一个集成账号,但是我们却希望通过这个接口创建的记录的创建者是其它用户。如果你有这个需求,你就需要用到用户模拟功能。

在Dynamics CRM Web API中,如果开启用户模拟功能非常简单,只需要在HTTP消息头加上一对额外的标记即可,例如:"MSCRMCallerID: {8CBD0DA3-AEAB-E611-80F4-C4346BC540C4}"。


下面我们看一个使用用户模拟功能创建一个客户记录的请求报文:

POST https://crmhomedev06.api.crm6.dynamics.com/api/data/v8.2/accounts HTTP/1.1
MSCRMCallerID: {8CBD0DA3-AEAB-E611-80F4-C4346BC540C4}
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlJyUXF1OXJ5ZEJWUldtY29jdVhVYjIwSEdSTSIsImtpZCI6IlJyUXF1OXJ5ZEJWUldtY29jdVhVYjIwSEdSTSJ9.eyJhdWQiOiJodHRwczovL2NybWhvbWVkZXYwNi5hcGkuY3JtNi5keW5hbWljcy5jb20vIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvYmI4Y2IyMmUtY2VhOC00ZTQwLWJiM2ItNmIxZGEwMzg4MDEwLyIsImlhdCI6MTQ3OTM2MTExNywibmJmIjoxNDc5MzYxMTE3LCJleHAiOjE0NzkzNjUwMTcsImFjciI6IjEiLCJhbXIiOlsicHdkIl0sImFwcGlkIjoiNTYzOTI3NDktZDlmOS00NzZmLWIyM2QtZmE3ZGZkYjdlZjQzIiwiYXBwaWRhY3IiOiIwIiwiZV9leHAiOjEwODAwLCJmYW1pbHlfbmFtZSI6Inhpb25nIiwiZ2l2ZW5fbmFtZSI6ImplZmYiLCJpcGFkZHIiOiIyMDcuNDYuMTM5LjI0OSIsIm5hbWUiOiJqZWZmIHhpb25nIiwib2lkIjoiNTg3ODk2NGUtNzVhMy00MmE5LTkyMTItMGY2YWFkMGEyN2U5IiwicGxhdGYiOiIzIiwicHVpZCI6IjEwMDM3RkZFOUJEMEY2QTIiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJzdWIiOiI0U3A4Z2htQUdBSlNvQnpJOFM0M1RJWmxzbHZQNTRFdFpxTEh3Qm44Y0ZjIiwidGlkIjoiYmI4Y2IyMmUtY2VhOC00ZTQwLWJiM2ItNmIxZGEwMzg4MDEwIiwidW5pcXVlX25hbWUiOiJqZWZmLnhpb25nQGNybWhvbWVkZXYwNi5vbm1pY3Jvc29mdC5jb20iLCJ1cG4iOiJqZWZmLnhpb25nQGNybWhvbWVkZXYwNi5vbm1pY3Jvc29mdC5jb20iLCJ2ZXIiOiIxLjAiLCJ3aWRzIjpbIjYyZTkwMzk0LTY5ZjUtNDIzNy05MTkwLTAxMjE3NzE0NWUxMCJdfQ.Kry_3Od5-OGw9tN25BOxGsgZNn4jJJruzYJeRJbTKJGYZpDx7zlHiAUyicvq0jPSYe0JOT6NHh8r_4DTQEgnIWirFyIh8-i2B5ZExm55fCXW6kuPp3eMtHbiDXpQfKROs_62UAMlOc0vw2P68rGuxEAoPVzBIKr5_fsi1OJt2F-d9CCCrPFM9puRtelpz1UT2B6pXJd-5a779LsEDV7VaX6WKtlhmOyWqAXujJ2_mgjR72EySoIGPBEgM1cYAztsjJHAeFAjryWWibo_ZwQwnfbnddRknY1tcF_R6CNYhJ2UIcThGgOITMFyAzNTpVGvSCuHghBIv2Wx9xISfrWGpg
Content-Type: application/json; charset=utf-8
Host: crmhomedev06.api.crm6.dynamics.com
Content-Length: 36
Expect: 100-continue

{"name":"[f]MCXlwn^UitPFofALaXGwS^"}

使用C#调用的测试代码如下:

  JObject account = new JObject();
            account.Add("name", StringGenerator(NumberGenerator()));

            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "accounts");
            req.Content = new StringContent(JsonConvert.SerializeObject(account), Encoding.UTF8, "application/json");
            req.Headers.Add("MSCRMCallerID", "{8CBD0DA3-AEAB-E611-80F4-C4346BC540C4}");

            HttpResponseMessage resp = await _client.SendAsync(req);

            return resp.IsSuccessStatusCode;


你可能感兴趣的:(Dynamics,CRM,Microsoft,Dynamics,CRM,编程系列)