Refer to this MSDN article on details on building a CrmConnection. The article includes details on how to update the web.config, which connection parameters are available, and how to configure the connection for different Microsoft Dynamics CRM 2011 environments/deployments.

The SDK for Microsoft Dynamics CRM 2011 uses the concept of a connection string to connect to the Microsoft Dynamics CRM server. This is similar to the concept of connection strings used with Microsoft SQL Server. The APIs in SDK for Microsoft Dynamics CRM work with a CrmConnection object, used internally by the CrmOrganizationServiceContext class, that uses a connection string to connect to the server. The connection string is specified in the app.config or web.config file for your project.

Parameters

The table below explains many of the Connection String configuration parameters used by Adxstudio Portals.

Name Description
Server, Url or ServiceUri Specifies the url to the CRM server. It can be http or https and the port is optional if it is http over port 80 or https over port 443. The server url is typically in the format of http://crm-server:port/organization-name or for CRM Online https://organization-name.crm.dynamics.com. The organization-name is required.
Domain The domain that will verify user credentials.
Username or UserID The user's identification name associated with the credentials.
Password The password for the user name associated with the credentials.
DeviceID or DeviceUserName The user-defined Windows Live Services Device ID. It can be any non-empty string. This device ID will be registered with the device password the first time an authentication is run. If the device ID and password do not match the values that have been previously registered then the authentication will fail.
DevicePassword  The user-defined Windows Live Services Device Password. It must be at least six characters in length. This device password will be registered with the device ID the first time an authentication is run. If the device ID and password do not match the values that have been previously registered then the authentication will fail.
Timeout  Gets passed to the Timeout property of the OrganizationServiceProxy class.
HomeRealmUri Gets passed to the HomeRealmUri property of the OrganizationServiceProxy class. 
ProxyTypesEnabled  True by default, this enables the OrganizationService to return proxy types instead of just the base Entity type.
ProxyTypesAssembly The assembly that contains the proxy types from Microsoft Dynamics CRM, generated using the CrmSvcUtil.exe tool, with the Microsoft.Xrm.Client.CodeGeneration extension.
CallerID Gets passed to the CallerId property of the OrganizationServiceProxy class.
ServiceConfigurationInstanceMode Can be set to Static, PerName, PerRequest, or PerInstance. PerName is default; this optimizes the number of times an IServiceConfiguration will be created for an OrganizationService, so that only one is created for each connection string. For more information about other possible values, see Specify the instanceMode Attribute.
UserTokenExpiryWindow A TimeSpan, formatted hh:mm:ss, used as an offset for when a new user token is retrieved. For example, if set to "00:10:00" a new user token will be retrieved 10 minutes before the token actually expires.

Examples

The following examples show the connection string for the various deployments and authentication schemes of Microsoft Dynamics CRM 2011.

Integrated On Premises

<connectionStrings>
	<add name="Xrm" connectionString="Url=http://crm2011.contoso.com/orgname;" />
</connectionStrings>

Active Directory

<connectionStrings>
	<add name="Xrm" connectionString="Url=http://crm2011.contoso.com/orgname; Domain=CONTOSO; Username=jdoe; Password=xxxxxx" />
</connectionStrings>

Claims/IFD

<connectionStrings>
	<add name="Xrm" connectionString="Url=https://orgname.contoso.com; Username=CONTOSO\jdoe; Password=xxxxxx" />
</connectionStrings>

CRM Online (Office 365)

<connectionStrings>
	<add name="Xrm" connectionString="Url=https://contoso.crm.dynamics.com; Username=john.doe@contoso.onmicrosoft.com; Password=xxxxxx"/>
</connectionStrings>

CRM Online (Live/Passport)

<connectionStrings>
	<add name="Xrm" connectionString="Url=https://contoso.crm.dynamics.com; Username=john.doe@live.com; Password=xxxxxx; DeviceID=13dd07sbxaj94r0fdolxg0q8; DevicePassword=xxxxxx"/>
</connectionStrings>

Protecting Connection Information

To improve the security of the connection information in the web.config file, the connection strings can be encrypted using the aspnet_regiis.exe tool.

  1. Ensure that the portal containing the web.config is already configured and hosted as under an IIS website. The aspnet_regiis.exe tool relies on the IIS website to locate the web.config file.
  2. Start a Command Prompt (cmd.exe) or PowerShell window making sure to Run as Administrator. Run this program on the web server machine hosting the website.
  3. Determine the IIS application by running the following command:
    1. C:\Windows\System32\inetsrv\AppCmd.exe list app
    2. For example:

      C:\WINDOWS\system32> C:\Windows\System32\inetsrv\AppCmd.exe list app
      APP "Default Web Site/" (applicationPool:DefaultAppPool)
      APP "portal.contoso.com/" (applicationPool:XRM)
    3. A list of the current applications are printed where the second column contains the website name ("portal.contoso.com") and the application name ("/") concatenated as a single string. Note these values.
  4. Determine the IIS website by running the following command:
    1. C:\Windows\System32\inetsrv\AppCmd.exe list site
    2. For example:

      C:\WINDOWS\system32> C:\Windows\System32\inetsrv\AppCmd.exe list site
      SITE "Default Web Site" (id:1,bindings:http/*:80:,net.tcp/808:*,net.pipe/*,net.msmq/localhost,msmq.formatname/localhost,state:Started)
      SITE "portal.contoso.com" (id:2,bindings:http/*:80:portal.contoso.com,http/*,state:Started
    3. Locate the website using the website name from the previous step and take note of the site ID value ("2").
  5. Use the site ID (example "2") and application name (example "/") to call aspnet_regiis.exe to encrypt the connection strings of the web.config.
    1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pe "connectionStrings" -site "2" -app "/"
    2. For example:

      C:\WINDOWS\system32> C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pe "connectionStrings" -site "2" -app "/"
      Microsoft (R) ASP.NET RegIIS version 4.0.30319.0
      Administration utility to install and uninstall ASP.NET on the local machine.
      Copyright (C) Microsoft Corporation.  All rights reserved.
      Encrypting configuration section...
      Succeeded!
  6. The connection strings is now rewritten in an encrypted format. If it is necessary to reverse the encryption to get back the original connection strings, run the following:
    1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pd "connectionStrings" -site "2" -app "/"
    2. Repeat step 5 to re-encrypt the connection strings after making any necessary changes.