我有一个 .NET MVC 应用程序,它将与 CRM 2016 Online 一起使用。我将 Azure AD 身份验证添加到项目中,它运行良好,因此用户可以使用与 CRM 相同的帐户登录它。如果他们已经登录,则没有登录屏幕。
问题是我想使用这些凭据通过组织服务或 Web Api 从 CRM 获取数据。
如何使用已有的登录信息并将其传递给服务?任何地方都有关于此的代码示例吗?
看来 ADAL 是最佳选择。我在这里尝试了这段代码 https://msdn.microsoft.com/en-us/library/gg327838.aspx
// Authenticate the registered application with Azure Active Directory.
AuthenticationContext authContext =
new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new
Uri(redirectUrl));
没有 AuthenticationContext。我添加了 ADAL NuGet 包来获取此内容。但失败了! AuthenticationContext 中没有 AcquireToken 方法。有一种称为 AcquireTokenASync,它具有完全不同的参数。所以我也被困在这里了!
如果这可行,它是否可以使用我已经完成的 Azure AD 登录,或者是否会弹出一个新的登录屏幕?
最佳答案
这可能不太适合您的情况,但您可以。
- 在服务帐户下运行您的 MVC 应用。
- 使用服务帐户凭据访问 CRM。
- 当有人登录您的 MVC 应用时,我想您会获得他们的电子邮件地址或域名等信息。
- 作为服务帐户,在 CRM 中搜索与电子邮件地址或域名匹配的
systemuser
记录。您需要找到系统用户记录Id。 - 创建另一个
IOrganizationService
,但传递您在 CRM 中找到的用户记录 ID。 - 实际上,您的服务帐户现在正在模拟登录 MVC 应用的用户。您所做的任何事情都将以模拟用户的身份完成。
// Retrieve the system user ID of the user to impersonate.
OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy);
_userId = (from user in orgContext.CreateQuery<SystemUser>()
where user.FullName == "Kevin Cook"
select user.SystemUserId.Value).FirstOrDefault();
// To impersonate another user, set the OrganizationServiceProxy.CallerId
// property to the ID of the other user.
_serviceProxy.CallerId = _userId;
Impersonate another user using the Web API
POST [Organization URI]/api/data/v8.1/accounts HTTP/1.1
MSCRMCallerID: 00000000-0000-0000-000000000002
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
{"name":"Sample Account created using impersonation"}
关于.net - 使用 Azure AD 凭据连接 CRM Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463830/