IIS:使用AppPool Identity访问Microsoft CRM 2013

时间:2015-01-19 09:44:04

标签: c# asp.net iis credentials dynamics-crm-2013

我的IIS MVC网站连接到Microsoft CRM 2013.用户/域/密码在web.config中设置,这很好。

现在我想删除web.config中的硬编码凭据。要访问CRM,我想使用 AppPool标识的凭据。此用户对MS CRM中的服务器和管理员角色拥有足够的权限。我试过这种方式:

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

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

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

这不起作用。 DefaultNetworkCredentials为空。

我需要帮助才能解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:0)

而不是自己创建凭据和组织服务代理,而是让CrmConfiguration处理这个:

您的Web.config将如下所示:

<?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>

您的代码将像这样连接到CRM:

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;
}

参考:Simplified connection to Microsoft Dynamics CRM