示例#1
0
        /// <summary>
        /// Generic method to obtain discovery/organization service proxy instance.
        /// </summary>
        /// <typeparam name="TService">
        /// Set IDiscoveryService or IOrganizationService type to request respective service proxy instance.
        /// </typeparam>
        /// <typeparam name="TProxy">
        /// Set the return type to either DiscoveryServiceProxy or OrganizationServiceProxy type based on TService type.
        /// </typeparam>
        /// <param name="serviceManagement">An instance of IServiceManagement</param>
        /// <param name="authCredentials">The user's Microsoft Dynamics CRM logon credentials.</param>
        /// <returns></returns>
        private TProxy GetProxy <TService, TProxy>(
            Microsoft.Xrm.Sdk.Client.IServiceManagement <TService> serviceManagement,
            Microsoft.Xrm.Sdk.Client.AuthenticationCredentials authCredentials)
            where TService : class
            where TProxy : Microsoft.Xrm.Sdk.Client.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 }));
        }
示例#2
0
        /*public IdentityProvider GetIdentityProvider(string userPrincipalName)
         * {
         *  return this.service.GetIdentityProvider(userPrincipalName);
         * }*/

        /*public SecurityTokenResponse Authenticate(
         * ClientCredentials clientCredentials,
         * Uri uri,
         * string keyType)
         * {
         *  return this.service.Authenticate(clientCredentials, uri, keyType);
         * }
         *
         * public SecurityTokenResponse Authenticate(
         * SecurityToken securityToken,
         * Uri uri,
         * string keyType)
         * {
         *  return this.service.Authenticate(securityToken, uri, keyType);
         * }*/

        public AuthenticationCredentials Authenticate(
            AuthenticationCredentials authenticationCredentials)
        {
            return(this.service.Authenticate(authenticationCredentials));
        }