CRM 2016 api未经授权的访问

时间:2016-09-22 19:31:26

标签: wcf dynamics-crm crm

我编写了这段代码,用于连接到2016年的CRM,并检索一些数据。

这是

var credentials = new NetworkCredential("username", "password?", "domain");
            var client = new HttpClient(new HttpClientHandler() { Credentials = credentials })
            {
                BaseAddress = new Uri("https://xxxtestenv.elluciancrmrecruit.com/api/data/v8.0/")
            };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var response = client.GetAsync("datatel_events?$orderby=datatel_eventname").Result;
            if (response.IsSuccessStatusCode)
            {
                var yourcustomobjects = response.Content.ReadAsStringAsync();
            }

但我收到此错误

  response    {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
     

{     REQ_ID:b685b007-199b-4a4a-85cc-3a29684e5588     日期:2016年9月22日星期四19:27:35 GMT     服务器:Microsoft-IIS / 8.5     WWW-Authenticate:谈判     WWW-Authenticate:NTLM     X-Powered-By:ASP.NET     连接:保持活力     内容长度:49     Content-Type:text / plain   System.Net.Http.HttpResponseMessage

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码连接到MS CRM。

string strOrganizationUri = "https://{your domain}/XRMServices/2011/Organization.svc";
            string strUserName = "{your domain username}";
            string strPassword = "{your domain password}";
            var organizationUriIFD = new Uri(strOrganizationUri);

            var credentials = new ClientCredentials();
            credentials.UserName.UserName = strUserName;
            credentials.UserName.Password = strPassword;

            IServiceConfiguration<IOrganizationService> config = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(organizationUriIFD);
            IOrganizationService service;
            using (var serviceProxy = new OrganizationServiceProxy(config, credentials))
            {
                // This statement is required to enable early-bound type support.
                serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                service = serviceProxy;

            }


            // Get the ID's of the current user and business unit.
            var who = new WhoAmIRequest();
            //retreive user data from the server.
            var whoResponse = (WhoAmIResponse)service.Execute(who);