2017-07-17 73 views
0

可能是重複的,但因爲我沒有找到確切的答案,所以我發佈了這個。動態CRM - 無法驗證Web API以使用數據?

我有我的代碼動態CRM Web API &憑據我使用它們如下:

string username = @"[email protected]"; 
    string password = @"XXXXXXXX"; 
    string domain = @"tmeictest.crm8.dynamics.com"; 
    string apiURL = @"https://tmeictest.api.crm8.dynamics.com/api/data/v8.2/"; 

然後,我使用初始化方法如下客戶端:

HttpClient client = GetNewHttpClient(username, password, domain, apiURL); 

public HttpClient GetNewHttpClient(string userName, string password, string domainName, string webAPIBaseAddress) 
    { 
     HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(userName, password, domainName) }); 
     client.BaseAddress = new Uri(webAPIBaseAddress); 
     client.Timeout = new TimeSpan(0, 2, 0); 
     return client; 
    } 

我打電話是爲了響應

HttpResponseMessage msg = client.GetAsync(apiURL).Result; 

但它給

未經授權狀態401

我在瀏覽器中直接檢查&我能登錄。但是,在我的代碼中使用它時,它不會進行身份驗證。

我在這裏錯過了什麼嗎?

回答

0

這裏是從該MSDN article含有C#很多的WebAPI樣本代碼的示例:

/// <summary> 
    /// Obtains the connection information from the application's configuration file, then 
    /// uses this info to connect to the specified CRM service. 
    /// </summary> 
    /// <param name="args"> Command line arguments. The first specifies the name of the 
    /// connection string setting. </param> 
    private void ConnectToCRM(String[] cmdargs) 
    { 
    //Create a helper object to read app.config for service URL and application 
    // registration settings. 
    Configuration config = null; 
    if (cmdargs.Length > 0) 
    { config = new FileConfiguration(cmdargs[0]); } 
    else 
    { config = new FileConfiguration(null); } 
    //Create a helper object to authenticate the user with this connection info. 
    Authentication auth = new Authentication(config); 
    //Next use a HttpClient object to connect to specified CRM Web service. 
    httpClient = new HttpClient(auth.ClientHandler, true); 
    //Define the Web API base address, the max period of execute time, the 
    // default OData version, and the default response payload format. 
    httpClient.BaseAddress = new Uri(config.ServiceUrl + "api/data/"); 
    httpClient.Timeout = new TimeSpan(0, 2, 0); 
    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); 
    httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0"); 
    httpClient.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json")); 
    } 
1

在問題將僅用於內部部署標準物質工作的代碼。我還沒有測試下面的解決方案,但你可以試試看。下面的客戶端ID將是您在使用AAD註冊CRM時會收到的客戶端ID。步驟here

var client = new HttpClient(); 

var authenticationContext = new AuthenticationContext(
    authenticationParameters.Authority, false); 

AuthenticationParameters authenticationParameters = 
    AuthenticationParameters.CreateFromResourceUrlAsync(
     "https://tmeictest.api.crm8.dynamics.com").Result; 

var userCredential = new UserCredential(@"[email protected]", @"XXXXXXXX"); 

AuthenticationResult authenticationResult = 
    authenticationContext.AcquireToken(
     authenticationParameters.Resource, @"" /* clientId */, userCredential); 

client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken); 

HttpResponseMessage msg = client.GetAsync(apiURL).Result;