Customer Engagement Web サービスで Office 365 ユーザーを認証する

このトピックは、Microsoft Online Services 環境で Dynamics 365 Customer Engagement (on-premises) にアクセスする顧客に適用されます。 組織 Web サービスまたは検索 Web サービスに接続するアプリケーションを開発するときに考慮にいれる Dynamics 365 Customer Engagement (on-premises) 複数の ID プロバイダーがあります。 これらのプロバイダは管理フェデレーション ドメインおよび Microsoft アカウントとして識別されます。 次に示す同じクラスとコードが、すべてのサポートされる ID プロバイダーと Dynamics 365 Customer Engagement (on-premises) 展開の種類で動作しますが、このトピックでは、管理ドメインとフェデレーション ID プロバイダーでの Dynamics 365 Customer Engagement (on-premises) Web サービスの認証について焦点を当てています。

簡素化された認定クラスを使用する

OrganizationServiceProxyDiscoveryServiceProxy クラスを Web サービスの認証に使用できます。
これらのプロキシ クラスの使用方法の詳細については、「クライアント プロキシ クラスを使用した認証」を参照してください。

Note

ServerConnection クラスを含むサンプル ヘルパー コードは現在は管理されておらず、今後削除される予定です。 代替としてに、CrmServiceClient のようなSDK アセンブリでサポートされている認証API を使用してください。

別の認証方法は、SDK で提供されるヘルパー ソース コードを使用することです。 ヘルパー コード: ServerConnection クラス に示される ServerConnection ヘルパー クラスは、認証のための GetOrganizationProxy および GetProxy メソッドを提供します。 ServerConnectionのソース コードでは、実際には GetOrganizationProxyGetProxy を呼び出すします。

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);
}

コードは、組織サービスの 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) に接続する