简体   繁体   English

如何用c#连接到dynamics crm?

[英]How to connect to dynamics crm with c#?

The Office365 authentication type and OrganizationServiceProxy Connection are now deprecated Link , which variant is the best to create a connection between dynamics 365 Online and c#. do you have any examples. Office365 身份验证类型和 OrganizationServiceProxy Connection 现已弃用Link ,哪种变体最适合在dynamics 365 Online和 c# 之间创建连接。您有任何示例吗?

Personally I suggest to use Client Id & ClientSecret from an Azure App Registration.我个人建议使用 Azure App Registration 中的 Client Id & ClientSecret。 This method is also compatible with the .NET Core SDK package (still in Alpha https://www.nuget.org/packages/Microsoft.Powerplatform.Cds.Client/ ) as this package does not support username&password authentication. This method is also compatible with the .NET Core SDK package (still in Alpha https://www.nuget.org/packages/Microsoft.Powerplatform.Cds.Client/ ) as this package does not support username&password authentication.

You can find many tutorials online to use a Client Id & Client Secret, you can start from this one https://blog.magnetismsolutions.com/blog/paulnieuwelaar/2020/04/24/dynamics-365-xrmtooling-connect-with-azure-app-registration-in-c-您可以在网上找到许多使用客户端 ID 和客户端密码的教程,您可以从这个https://blog.magnetismsolutions.com/blog/paulnieuwelaar/2020/04/24/dynamics-365-xrmtooling-connect-with 开始-azure-app-registration-in-c-

You can still use a service account from your AD or over OAuth and a connection over the CRMServiceClient.您仍然可以使用 AD 中的服务帐户或通过 OAuth 和通过 CRMServiceClient 的连接。

Create a connection string in your web.config在 web.config 中创建一个连接字符串

 <add key="Dynamics365Test" value="AuthType=AD;Domain=domain;Username=domain\xxx;Password=XXXXX;Url=https://yourcrm.crm.dynamics.com/"/>

or

  <add name="Dynamics365Test" connectionString="AuthType=OAuth;Username=test@contoso.onmicrosoft.com;Password=passcode;Url=https://yourcrm.crm.dynamics.com;AppId=xxx-12ee-xxxe-aaae-xxx91f45987d;RedirectUri=app://xxx-0C36-4500-8xxx-xxx54F2AC97;TokenCacheStorePath=c:\MyTokenCache;LoginPrompt=Auto"/>

As I understand it, there are two primary ways to obtain a user with which to connect to Dynamics 365:据我了解,有两种主要方法可以获取用于连接到 Dynamics 365 的用户:

  • Use an existing AD service account and add it to Dynamics.使用现有的 AD 服务帐户并将其添加到 Dynamics。 The account will need to have MFA disabled to be used in a non-interactive context.该帐户需要禁用 MFA 才能在非交互式上下文中使用。 No app registration is needed.无需应用程序注册。
  • Register your app in Azure and create a user principal with client ID/secret or a certificate.在 Azure 中注册您的应用,并使用客户端 ID/密码或证书创建用户主体。

Dynamics 365 data is stored within a Dataverse database, and the available security roles for accessing that data are described here . Dynamics 365 数据存储在 Dataverse 数据库中, 此处描述了用于访问该数据的可用安全角色。 You'll need to ensure your user is in an appropriate role for data access, such as the 'Service Reader' role if you just need to read entities.您需要确保您的用户具有适当的数据访问角色,例如,如果您只需要读取实体,则为“服务读者”角色。

The Power Platform API and OAuth can be used to access the data with .NET. (See Organization Service ). Power Platform API 和 OAuth 可用于访问 .NET 的数据。(请参阅组织服务)。 These days ServiceClient is recommended over the older CrmServiceClient .现在推荐使用ServiceClient而不是旧的CrmServiceClient Here's a code sample using the service account approach:下面是使用服务帐户方法的代码示例:

Web.config: Web.config:

<add name="MyCrmEntities" connectionString="AuthType=OAuth;Username=serviceAccount@company.com;Password=MyPassword;Url=https://appname.crm.dynamics.com;LoginPrompt=Never"/>

Note: Recommendation is to not store the password in plain text as done for this example.注意:建议不要像本例那样以纯文本形式存储密码。 Instead, consider encrypting it or storing it in a more secure location.相反,请考虑对其进行加密或将其存储在更安全的位置。

You can see more connection string parameters here , including client ID/secret if those are relevant for your connection method.您可以在此处查看更多连接字符串参数,包括与您的连接方法相关的客户端 ID/密码。

C# (Retrieve a list of all 'Account' entities and output their names): C#(检索所有“帐户”实体的列表及其名称 output):

using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

...

using (ServiceClient service = new 
       ServiceClient(ConfigurationManager.ConnectionStrings["MyCrmEntities"].ConnectionString))
{
    if (service.IsReady)  // successfully connected to environment's Organization service
    {
        QueryExpression queryAccounts = new QueryExpression("account");
        queryAccounts.ColumnSet.AddColumns("name");
        EntityCollection accountResult = service.RetrieveMultiple(queryAccounts);

        foreach (Entity account in accountResult.Entities)
        {
            Console.WriteLine(account.Attributes["name"].ToString());   
        }
    }
}

I can't connect to CRM online with OAuth CrmServiceClient.我无法使用 OAuth CrmServiceClient 在线连接到 CRM。 Whenever I try it times out.每当我尝试超时。 I'm going mad with this.!!我要疯了。 My test code is below.我的测试代码如下。

Does the user need particular permissions or anything?用户是否需要特定权限或其他什么? (.Net version 4.6.2) (.Net 版本 4.6.2)

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Tooling.Connector;使用 Microsoft.Xrm.Tooling.Connector; using System;使用系统;

public partial class c2: System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { public partial class c2: System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

    string ConnectionString = "AuthType=OAuth;Username=xxx@xxx.com;Password=xxx;Url=https://xxx.crm4.dynamics.com;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Never";

    CrmServiceClient svc = new CrmServiceClient(ConnectionString);

    if (svc.IsReady)
    {
        var myContact = new Entity("contact");
        myContact.Attributes["lastname"] = "Test";
        myContact.Attributes["firstname"] = "User1";
        svc.Create(myContact);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM