2017-08-02 39 views
12

我有一個需要與Dynamic CRM 365 Web API交流的Web應用程序。動態CRM在ADFS上被配置爲依賴方。 服務器是Windows Server 2016,所有內容都是在內部部署,而不是在Azure上。將WebApp授權給ADFS以訪問Dynamics CRM Web API

我做什麼,以獲得有效的標記有以下幾種:

1)在ADFS去應用程序組,並添加新的服務器應用程序,採取了客戶端ID,也爲我的Web應用程序客戶端機密。

enter image description here

2)在Active Directory中添加新的新用戶webAppUser

3)在CRM添加該用戶爲應用用戶與應用程序ID,當我註冊了我的網絡我剛纔得到了客戶端ID應用程序到ADFS。還創建了一個新的角色,具有完全權限的實體帳戶,並將此角色分配給此應用程序用戶

4)我使用下面的代碼來檢索不記名令牌並將其添加到我的HttpClient授權標頭。

public class CrmWebApiClient 
{ 
    private HttpClient _httpClient; 

    public CrmWebApiClient() 
    { 
     _httpClient = new HttpClient(); 
     _httpClient.BaseAddress = new Uri("https://crmbaseaddress.com");    
    } 

    internal async Task Initialize() 
    { 
     try 
     {    
      var authority = "https://adfsServerUrl/adfs/"; 
      var authContext = new AuthenticationContext(authority,false); 
      var credentials = new ClientCredential(clientID,clientSecret); 

      var authResult = await authContext.AcquireTokenAsync("https://crmbaseaddress.com", credentials); 

      _httpClient.DefaultRequestHeaders.Authorization = 
       new AuthenticationHeaderValue("Bearer", authResult.AccessToken); 
     } 
     catch (Exception ex) 
     { 
      var error = ex; 
     } 

    } 

    internal async Task<string> GetValuesAsync() 
    { 
     var result = string.Empty; 
     try 
     { 
      result = await _httpClient.GetStringAsync("api/data/v8.1/accounts"); 
     } 
     catch (Exception ex) 
     { 
      var error = ex; 
     } 

     return result; 
    } 
} 

5)我設法得到一個令牌,但是當我打電話給CRM的Web Api時,我仍然得到401 Unauthorized。

你能幫我嗎?我在正確的道路上嗎?我應該做更多的事嗎?

回答

2

最後我不得不使用系統的用戶和使用的代碼,以獲得有效令牌發送它的憑證在我的OAuth請求如下:

namespace TestApp.App_Start { 
public class CrmWebApiClient 
{ 
    private HttpClient _httpClient; 

    public CrmWebApiClient() 
    {  
     _httpClient = new HttpClient(); 
     _httpClient.BaseAddress = new Uri("https://crmUrl"); 
     _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     _httpClient.DefaultRequestHeaders.Add("OData-MaxVersion","4.0"); 
     _httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0"); 
    } 

    internal async Task Initilize() 
    { 
     try 
     { 

      var tokenClient = new HttpClient();    
      var content = new FormUrlEncodedContent(new[] { 
       new KeyValuePair<string,string>("client_id",_clientID), 
       new KeyValuePair<string,string>("client_secret",_clientSecret), 
       new KeyValuePair<string,string>("resource",_urlOfResource), 
       new KeyValuePair<string,string>("username",_usernameOfSystemUser), 
       new KeyValuePair<string,string>("password",_passwordOfSystemUser), 
       new KeyValuePair<string,string>("grant_type","password"), 
      }); 
      var res = tokenClient.PostAsync("https://adfsUrl/adfs/oauth2/token", content); 
      var respo = res.Result.Content.ReadAsStringAsync().Result; 
      var accesstoken = JObject.Parse(respo).GetValue("access_token").ToString(); 

      _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesstoken); 

     } 
     catch (Exception ex) 
     { 
      Trace.WriteLine($"Exception when requesting the bearer token from ADFS: {ex.Message} - {ex.InnerException?.Message}"); 
     } 

    } 

    internal async Task<string> GetAccountsAsync() 
    { 
     var result = string.Empty; 
     try 
     { 
      result = _httpClient.GetStringAsync("/api/data/v8.0/accounts").Result; 

     } 
     catch (Exception ex) 
     { 
      Trace.WriteLine($"Exception when calling the CRM api: {ex.Message} - {ex.InnerException?.Message}"); 
     } 
     return result; 
    } 
} 
} 


// Use the above class like that 
var httpClient = new CrmWebApiClient(); 
httpClient.Initilize().Wait(); 
var result = httpClient.GetAccountsAsync().Result; 
+0

什麼版本的ADFS的這是什麼?是否支持密碼授權..顯然有,但你能否澄清一點。 – davidcarr

+1

@davidcarr這是Windows Server 2016與最新的ADFS –

+0

@RickyStam我有類似的情況,你能澄清你的意思是系統用戶嗎?並非所有用戶都是用戶創建的? –