In this article

Helper code: ServerConnection class

12/31/2017

25 minutes to read

Contributors

In this article

Applies to Dynamics 365 (online), version 9.x

The primary purpose of the ServerConnection class is to show how to connect to the Dynamics 365 (online) Customer Engagement web services to invoke web methods. In addition, applications must typically perform other tasks such as obtaining server and organization information, obtaining user login credentials, creating a service proxy, and refreshing the WCF connection security token. The ServerConnection class provides this needed functionality.

Caution

The ServerConnection class is used by most samples that are included in the Dynamics 365 Customer Engagement Web Services. The class is updated periodically with new functionality. Do not simply reuse the helper code for authentication in your applications. It is code that the Dynamics 365 Customer Engagement Web Services uses to provide the optimum experience when you run our Console application samples included in the SDK. It contains all the key elements of authentication and demonstrates their use, but it may not represent the best solution for your application. It is sample code that you can use as a basis for designing an authentication management system that fits the requirements of your application. Refer to the topic Use connection strings in XRM tooling to connect to Dynamics 365 for an alternate method for authenticating with the web services.

The file is present in code samples' folder that you download.

Path: <download-directory>\<sample name>\C#\CrmServiceHelpers.cs.

For example, if you download the Work with attribute metadata sample to D drive, you can find the sample in the following folder path.

Path: D:\Work with attribute metadata\CrmServiceHelpers.cs

Use the class source code in the CrmServiceHelpers files as a basis for your own classes or use the CrmServiceClient class for just the basic functionality of setting up a service connection.

Requirements

Demonstrates

The ServerConnection class demonstrates how to establish a connection to the Dynamics 365 web services for the purpose of invoking web methods. Before a connection can be created, information about the server, organization, and user must first be obtained. This information is gathered in either of two ways: by interactively prompting the user in the console window, or by loading saved server configuration information from the local disk. The server connection information is persisted to a file named Credentials.xml located at C:\Users\<username>\AppData\Roaming\CrmServer.

For each server configuration saved in the Credentials.xml file, the user’s logon password is stored as a generic credential in the Windows Credential Manager. The credential naming format is Microsoft_CRMSDK:<server>:<organization>:<userID>.

Useful code for authentication can be found in the GetProxy and GetOrganizationProxy methods. Also, the code that creates and reads a user’s password in the Windows Credential Manager may be of interest.