2017-08-02 42 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我有类似的情况,你能澄清你的意思是系统用户吗?并非所有用户都是用户创建的? –