このトピックは、Microsoft Online Services 環境を通じて Dynamics 365 Customer Engagement (on-premises) にアクセスする顧客に適用されます。 Dynamics 365 Customer Engagement (on-premises) の ID プロバイダーは複数あり、組織 Web サービスまたは探索 Web サービスに接続するアプリケーションを開発するときに考慮する必要があります。 これらのプロバイダーは、マネージド ドメイン、フェデレーション、Microsoft アカウントとして識別できます。 このトピックでは、管理対象ドメインとフェデレーション ID プロバイダーを使用した Dynamics 365 Customer Engagement (on-premises) Web サービス認証に焦点を当てていますが、ここに示すのと同じクラスとコードは、サポートされているすべての ID プロバイダーと Dynamics 365 Customer Engagement (on-premises) 展開の種類でも機能します。
簡略化された認証クラスを使用する
OrganizationServiceProxy クラスと DiscoveryServiceProxy クラスは、Web サービスで認証するときに使用できます。
これらのプロキシ・クラスの使用の詳細については 、クライアント・プロキシ・クラスを使用した認証を参照してください。
注
ServerConnection
クラスを含むサンプル ヘルパー コードは保守されなくなり、将来削除される予定です。 代わりに、SDK アセンブリでサポートされている認証 API ( CrmServiceClient など) のいずれかを使用します。
別の認証方法は、SDK で提供されているヘルパーのソース コードを使用することです。 ServerConnection
ヘルパー クラス (「ヘルパー コード : ServerConnection クラス」を参照) は、認証のための GetOrganizationProxy
メソッドと GetProxy
メソッドを提供します。
ServerConnection
のソースコードを見ると、GetOrganizationProxy
実際にGetProxy
を呼び出していることがわかります。
using ( OrganizationServiceProxy orgServiceProxy = ServerConnection.GetOrganizationProxy(serverConfig) ) { }
これらの組織または探索サービスのプロキシ オブジェクトを using
ステートメントで作成して、サービス プロキシを正しく破棄するか、 Dispose
を直接呼び出す必要があります。 GetOrganizationProxy
ヘルパー コード メソッドを使用するサンプル コードについては、「サンプル : クイック スタート」を参照してください。
SDK アセンブリで使用できる認証クラスの完全な一覧は、「 認証クラス 」セクションに示されています。
Office 365 で Microsoft アカウント ユーザーを認証する
アプリケーションは、組織が Microsoft アカウント ID プロバイダーから Microsoft Online Services ID プロバイダーに移行された Dynamics 365 Customer Engagement (on-premises) ユーザーをサポートする必要があります。 このシナリオでは、ユーザーは Dynamics 365 Customer Engagement (on-premises) の Microsoft Online Services ID プロバイダーで認証するときに、Microsoft アカウントのサインイン資格情報を提供できます。
これを行うには、OrganizationServiceProxyコンストラクタまたはIServiceManagement
クラスのAuthenticationCredentialsメソッドに入力された資格情報を渡します。 資格情報の値は次のように入力されます。
AuthenticationCredentials.ClientCredentials = <Microsoft account sign-in credentials>
AuthenticationCredentials.SupportingCredentials.ClientCredentials = <device credentials>
コードで ID プロバイダーの種類を確認して認証方法を決定する場合は、追加のコードが必要です。 移行した Microsoft アカウント ユーザーをサポートするサンプル コードについては、次のセクションの GetCredentials
メソッドを参照してください。
この移行の詳細については、「 Dynamics 365 Customer Engagement (on-premises) と Office 365 の統合」を参照してください。
より複雑な認証
前の説明では、Dynamics 365 Customer Engagement (on-premises) Web サービスでユーザーを認証するために使用できる 2 つの簡単なアプローチを紹介しました。 次の情報は、 IServiceManagement<TService> クラスを使用してユーザーを認証する方法を示し、 GetProxy
メソッドのソース コードが含まれています。
次の例を含む完全なサンプルについては、「 サンプル: Office 365 ユーザーの認証」を参照してください。 このレベルでの認証には、より多くのコードが必要であることに気付くでしょう。
次のサンプル コードは、Dynamics 365 Customer Engagement (on-premises) Web サービスを使用して Office 365/MOS ユーザーを認証するためにアプリケーションで使用できるクラスとメソッドを示しています。
IServiceManagement<IOrganizationService> orgServiceManagement =
ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
new Uri(organizationUri));
// Set the credentials.
AuthenticationCredentials credentials = GetCredentials(orgServiceManagement, endpointType);
// Get the organization service proxy.
using (OrganizationServiceProxy organizationProxy =
GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceManagement, credentials))
{
// This statement is required to enable early-bound type support.
organizationProxy.EnableProxyTypes();
// Now make an SDK call with the organization service proxy.
// Display information about the logged on user.
Guid userid = ((WhoAmIResponse)organizationProxy.Execute(
new WhoAmIRequest())).UserId;
SystemUser systemUser = organizationProxy.Retrieve("systemuser", userid,
new ColumnSet(new string[] { "firstname", "lastname" })).ToEntity<SystemUser>();
Console.WriteLine("Logged on user is {0} {1}.",
systemUser.FirstName, systemUser.LastName);
}
このコードは、Organization サービスの IServiceManagement<TService> オブジェクトを作成します。 AuthenticationCredentials 型のオブジェクトは、ユーザーのサインイン資格情報を格納するために使用されます。 その後、 IServiceManagement
オブジェクトとユーザー資格情報が GetProxy
に渡され、Web サービス・プロキシー参照が取得されます。
/// <summary>
/// Obtain the AuthenticationCredentials based on AuthenticationProviderType.
/// </summary>
/// <param name="service">A service management object.</param>
/// <param name="endpointType">An AuthenticationProviderType of the CRM environment.</param>
/// <returns>Get filled credentials.</returns>
private AuthenticationCredentials GetCredentials<TService>(IServiceManagement<TService> service, AuthenticationProviderType endpointType)
{
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
switch (endpointType)
{
case AuthenticationProviderType.ActiveDirectory:
authCredentials.ClientCredentials.Windows.ClientCredential =
new System.Net.NetworkCredential(_userName,
_password,
_domain);
break;
case AuthenticationProviderType.LiveId:
authCredentials.ClientCredentials.UserName.UserName = _userName;
authCredentials.ClientCredentials.UserName.Password = _password;
authCredentials.SupportingCredentials = new AuthenticationCredentials();
authCredentials.SupportingCredentials.ClientCredentials =
Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
break;
default: // For Federated and OnlineFederated environments.
authCredentials.ClientCredentials.UserName.UserName = _userName;
authCredentials.ClientCredentials.UserName.Password = _password;
// For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
// authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName; // Windows Kerberos
// The service is configured for User Id authentication, but the user might provide Microsoft
// account credentials. If so, the supporting credentials must contain the device credentials.
if (endpointType == AuthenticationProviderType.OnlineFederation)
{
IdentityProvider provider = service.GetIdentityProvider(authCredentials.ClientCredentials.UserName.UserName);
if (provider != null && provider.IdentityProviderType == IdentityProviderType.LiveId)
{
authCredentials.SupportingCredentials = new AuthenticationCredentials();
authCredentials.SupportingCredentials.ClientCredentials =
Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
}
}
break;
}
return authCredentials;
}
AuthenticationCredentialsオブジェクトは、サインインしているユーザーのサブスクライブされた ID に従って設定されます。 すべての種類の ID プロバイダーのユーザー資格情報が表示されています。 既定のケースでは、Office 365/MOS マネージド ドメイン、ID がクラウドでフェデレーションされているオンライン ユーザー、および移行された Microsoft アカウント ユーザーが処理されます。 では、 GetProxy
実際に何をしているのか見ていきましょう。
private TProxy GetProxy<TService, TProxy>(
IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
where TService : class
where TProxy : ServiceProxy<TService>
{
Type classType = typeof(TProxy);
if (serviceManagement.AuthenticationType !=
AuthenticationProviderType.ActiveDirectory)
{
AuthenticationCredentials tokenCredentials =
serviceManagement.Authenticate(authCredentials);
// Obtain discovery/organization service proxy for Federated, LiveId and OnlineFederated environments.
// Instantiate a new class of type using the 2 parameter constructor of type IServiceManagement and SecurityTokenResponse.
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
.Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
}
// Obtain discovery/organization service proxy for ActiveDirectory environment.
// Instantiate a new class of type using the 2 parameter constructor of type IServiceManagement and ClientCredentials.
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
.Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
}
オンプレミス (Active Directory、要求なし) 以外のすべてのデプロイでは、 Authenticate(AuthenticationCredentials) メソッドが呼び出され、サービス プロキシがインスタンス化されます。 Authenticate
から返される認証資格情報には、サービス プロキシ コンストラクタで使用されるセキュリティ トークン応答が含まれていることに注意してください。 前に示した汎用の GetProxy
メソッドを使用して、 OrganizationServiceProxy または DiscoveryServiceProxy へのオブジェクト参照を取得できます。
こちらも参照ください
Microsoft Office 365 および Dynamics 365 Customer Engagement (on-premises)との接続サンプル: Office 365 ユーザーの認証
ヘルパー コード: ServerConnection クラス
Active Directory とクレームベース認証
XRM ツールで接続文字列を使用して Dynamics 365 Customer Engagement (on-premises) に接続する