如何在没有提示的情况下通过ADAL,NetStandard和Azure AD对CRM Dynamics Online Web服务进行身份验证 [英] How to authenticate without prompt to CRM Dynamics Online webservices with ADAL, NetStandard, and Azure AD
问题描述
我目前正在尝试创建Xamarin应用,以便从Dynamics 365在线实例获取一些信息。通过AD进行身份验证并访问CRM API的代码将被逐出NetStandard(v1.6)库。
I'm currently trying to create a Xamarin App in order to get some info from a Dynamics 365 online instance. The code that authenticate with AD and access the CRM api is deported in a NetStandard (v1.6) Library.
我使用以下NuGets:
I use the following NuGets :
- Microsoft.IdentityModel.Clients.ActiveDirectory(3.13.9)
- NETStandard.Library(1.6.1) >
- Microsoft.IdentityModel.Clients.ActiveDirectory (3.13.9)
- NETStandard.Library (1.6.1)
我按照以下教程进行操作,以便将AD与我的Dynamics实例链接:> https://nishantrana.me/2016/11/13/register-a-dynamics- 365-app-with-azure-active-directory /
I followed the following tutorial in order to link AD with my Dynamics instance : https://nishantrana.me/2016/11/13/register-a-dynamics-365-app-with-azure-active-directory/
这是我的ActiveDirectory助手:
Here is my ActiveDirectory helper :
public static class ADHelper
{
public async static Task<AuthenticationResult> GetAuthAsync(Uri uri, ClientCredential creditential)
{
AuthenticationParameters ap = await AuthenticationParameters.CreateFromResourceUrlAsync(uri);
String authorityUrl = ap.Authority;
String resourceUrl = ap.Resource;
AuthenticationResult result = null;
AuthenticationContext authContext = new AuthenticationContext(authorityUrl, false);
result = await authContext.AcquireTokenAsync(resourceUrl, creditential);
return result;
}
}
我的CRM API客户端:
And my CRM API Client :
public class CRMClient
{
private AuthenticationResult Auth { get; set; }
private Uri baseUri { get; set; }
public CRMClient(Uri uri, ClientCredential creditential)
{
baseUri = uri;
Auth = ADHelper.GetAuthAsync(uri, creditential).Result;
}
public void getObject()
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Auth.AccessToken);
client.Timeout = new TimeSpan(0, 2, 0);
client.BaseAddress = baseUri;
HttpResponseMessage message = client.GetAsync("/accounts").Result;
String content = message.Content.ReadAsStringAsync().Result;
}
}
用于CRMClient构造函数的参数:
Parameters used for CRMClient Constructor :
- https:// [my_crm_instance_Url] /api/data/v8.2/ =>数据OData端点
- ClientCredential(AppId,AppKey)=>作为凭据
- https://[my_crm_instance_Url]/api/data/v8.2/ => the data OData endpoint
- ClientCredential(AppId, AppKey) => as credential
Azure AD给了我一个令牌,但是UserInfo,TenantId和idToken都为 null
(这可能是
Azure AD gives me a token back, but UserInfo, TenantId and idToken are all null
(This could be a part of the cause of my problem).
当前,返回的内容是HTML office 365登录页面,而不是我想要获取的数据。
Currently, the returned content is the HTML office 365 login page instead of the data I wanted to get.
有人可以帮我吗?
推荐答案
我在此处创建了完整的博客文章
I create a full blog post here
http://phuocle.net/crm/dynamics-365-online-s2s-authentication-full-explain.aspx
这篇关于如何在没有提示的情况下通过ADAL,NetStandard和Azure AD对CRM Dynamics Online Web服务进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!