1

My IIS MVC web site connects to Microsoft CRM 2013. User/Domain/Password are set in web.config, this works fine.

Now I want to remove hard coded credentials in web.config. To Access CRM I want to use credentials of AppPool Identity. This user has enough rights on server and administrator role in MS CRM. I tried this way:

Uri organizationUri = new Uri(WebConfigurationManager.AppSettings["CrmUrl"]);

var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

var orgProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);

This does not work. DefaultNetworkCredentials are empty.

I need help to manage this problem. Thanks!

Mat
  • 51
  • 5

1 Answers1

0

Instead of creating the credentials and the organization service proxy yourself, rather let CrmConfiguration handle this:

Your Web.config will look something like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
  </configSections>

  <connectionStrings>
  <!-- On-Premise with integrated Authentication -->
      <add name="MyOnPremiseConnection" connectionString="Url=http://SERVER-NAME/ORG-NAME/XRMServices/2011/Organization.svc;" />
  </connectionStrings>

  <microsoft.xrm.client>
    <contexts default="MyOnPremiseContext">
      <!-- On Premise CRM-->
      <add name="MyOnPremiseContext" connectionStringName="MyOnPremiseConnection" serviceName="MyService" />
    </contexts>

    <services default="MyService">
      <add name="MyService" serviceCacheName="Xrm" />
    </services>

    <serviceCache default="Xrm">
      <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationServiceCache, Microsoft.Xrm.Client" cacheMode="Disabled" />
    </serviceCache>
  </microsoft.xrm.client>
</configuration>

Your code will connect to CRM like this:

using (var servicecontext = CrmConfigurationManager.CreateContext("MyOnPremiseConnection", true) as CrmOrganizationServiceContext)
{
    var query = new QueryExpression
    {
        EntityName = "account",
        ColumnSet = new ColumnSet("name"),
        TopCount = 10,
    };

    var top10accounts = servicecontext.RetrieveMultiple(query).Entities;
}

Reference: Simplified connection to Microsoft Dynamics CRM

Filburt
  • 17,626
  • 12
  • 64
  • 115