in apps/samplecode/automotive/apisamples/Microsoft.Dynamics.Automotive.Samples/helpercode/CrmServiceHelpers.cs [737:859]
public static TProxy GetProxy<TService, TProxy>(ServerConnection.Configuration currentConfig)
where TService : class
where TProxy : ServiceProxy<TService>
{
// Check if it is organization service proxy request.
Boolean isOrgServiceRequest = typeof(TService).Equals(typeof(IOrganizationService));
// Get appropriate Uri from Configuration.
Uri serviceUri = isOrgServiceRequest ?
currentConfig.OrganizationUri : currentConfig.DiscoveryUri;
// Set service management for either organization service Uri or discovery service Uri.
// For organization service Uri, if service management exists
// then use it from cache. Otherwise create new service management for current organization.
IServiceManagement<TService> serviceManagement =
(isOrgServiceRequest && null != currentConfig.OrganizationServiceManagement) ?
(IServiceManagement<TService>)currentConfig.OrganizationServiceManagement :
ServiceConfigurationFactory.CreateManagement<TService>(
serviceUri);
if (isOrgServiceRequest)
{
if (currentConfig.OrganizationTokenResponse == null)
{
currentConfig.OrganizationServiceManagement =
(IServiceManagement<IOrganizationService>)serviceManagement;
}
}
// Set the EndpointType in the current Configuration object
// while adding new configuration using discovery service proxy.
else
{
// Get the EndpointType.
currentConfig.EndpointType = serviceManagement.AuthenticationType;
// Get the logon credentials.
currentConfig.Credentials = GetUserLogonCredentials(currentConfig);
}
// Set the credentials.
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
// If UserPrincipalName exists, use it. Otherwise, set the logon credentials from the configuration.
if (!String.IsNullOrWhiteSpace(currentConfig.UserPrincipalName))
{
// Single sing-on with the Federated Identity organization using current UserPrinicipalName.
authCredentials.UserPrincipalName = currentConfig.UserPrincipalName;
}
else
{
authCredentials.ClientCredentials = currentConfig.Credentials;
}
Type classType;
// Obtain discovery/organization service proxy for Federated,
// Microsoft account and OnlineFederated environments.
if (currentConfig.EndpointType !=
AuthenticationProviderType.ActiveDirectory)
{
if (currentConfig.EndpointType == AuthenticationProviderType.LiveId)
{
authCredentials.SupportingCredentials = new AuthenticationCredentials();
authCredentials.SupportingCredentials.ClientCredentials =
currentConfig.DeviceCredentials;
}
AuthenticationCredentials tokenCredentials =
serviceManagement.Authenticate(
authCredentials);
if (isOrgServiceRequest)
{
// Set SecurityTokenResponse for the current organization.
currentConfig.OrganizationTokenResponse = tokenCredentials.SecurityTokenResponse;
// Set classType to ManagedTokenOrganizationServiceProxy.
classType = typeof(ManagedTokenOrganizationServiceProxy);
}
else
{
// Set classType to ManagedTokenDiscoveryServiceProxy.
classType = typeof(ManagedTokenDiscoveryServiceProxy);
}
// Invokes ManagedTokenOrganizationServiceProxy or ManagedTokenDiscoveryServiceProxy
// (IServiceManagement<TService>, SecurityTokenResponse) constructor.
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.
if (isOrgServiceRequest)
{
classType = typeof(ManagedTokenOrganizationServiceProxy);
}
else
{
classType = typeof(ManagedTokenDiscoveryServiceProxy);
}
// Invokes ManagedTokenDiscoveryServiceProxy or ManagedTokenOrganizationServiceProxy
// (IServiceManagement<TService>, ClientCredentials) constructor.
return (TProxy)classType
.GetConstructor(new Type[]
{
typeof(IServiceManagement<TService>),
typeof(ClientCredentials)
})
.Invoke(new object[]
{
serviceManagement,
authCredentials.ClientCredentials
});
}