2016-09-22 116 views
-1

我編寫了此代碼,以在2016年連接到CRM並檢索一些數據。CRM 2016 api未經授權訪問

這是

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身份驗證:協商 WWW身份驗證:NTLM X -Powered-者:ASP.NET 連接:保活 的Content-Length:49 的Content-Type:text/plain的 }} System.Net.Http.HttpResponseMessage

有什麼建議?

+0

它是否啓用了IFD? –

+0

是的,我們可以從任何地方訪問它。當我調試httpclient將鏈接重定向到我們用來登錄crm的另一個鏈接的代碼時發現。這聽起來對你來說很正常嗎? –

回答

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);