连接到CRM

Dan*_*nia 5 c# asp.net dotnetnuke crm web-config

我需要我的网站连接到CRM这是我的代码

        var organizationUri = new Uri(ConfigurationManager.AppSettings["CRMServerUrl"]);

        //Client credentials
        var credentials = new ClientCredentials();
        credentials.UserName.UserName = @"<user>";
        credentials.UserName.Password = "<password>";
        // Use the Microsoft Dynamics CRM Online connection string from the web.config file named "CrmConnectionStr".
        using (OrganizationServiceProxy _service = new OrganizationServiceProxy(organizationUri, null, credentials, null))
        {
            Response.Write("Connected");
        }
Run Code Online (Sandbox Code Playgroud)

这是在我的web.config中

 <add key="CRMServerUrl" value="http://XXXXX/XRMServices/2011/Organization.svc" />
<add key="username" value="<user>" />
<add key="password" value="<password>" />
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误信息:

"发生了严重错误.无法与CRM服务器连接.调用者未通过该服务进行身份验证."

Ale*_*lex 5

您应该使用►Simplified连接由提供►Microsoft.Xrm.Client.CrmConnection类型更容易的体验.

步骤很简单:

1)在.config文件中添加一个连接字符串

<connectionStrings>
  <add name="CrmConnStr" connectionString="!SEE EBELOW!"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

2)在代码中,它是这样的:

// parameter is the name of the connection string
// NOTE: These "setup" declarations are slow, reuse them as much as possibile
var connection = new CrmConnection("CrmConnStr");

var service = new OrganizationService(connection);
var context = new CrmOrganizationServiceContext(connection);
Run Code Online (Sandbox Code Playgroud)

关于连接字符串,如果On-Premise没有IFD它应该是

"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
<!-- https is always nice, but it's not mandatory in this case -->
Run Code Online (Sandbox Code Playgroud)

如果使用IFD或在线进行内部部署则应该是

"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
<!-- https is of course mandatory here -->
<!-- 'DOMAIN=...' part is not needed because of ADFS -->
Run Code Online (Sandbox Code Playgroud)


小智 5

在项目中使用这些程序集:

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client.Services;
Run Code Online (Sandbox Code Playgroud)

创建组织服务:

string connectionString = "Url=https://your_orgnisation.crm5.dynamics.com; Username=user_name; Password=your_password;";
CrmConnection connection = CrmConnection.Parse(connectionString);
OrganizationService organisationservice = new OrganizationService(connection);
Run Code Online (Sandbox Code Playgroud)

别忘了导入System.Runtime.serialization.


Dot*_*Pro 4

By looking at your code, you are not using username and password Change below lines:

    //Client credentials
    var credentials = new ClientCredentials();
    credentials.UserName.UserName =ConfigurationManager.AppSettings["username"].toString();
    credentials.UserName.Password =ConfigurationManager.AppSettings["password"].toString();
Run Code Online (Sandbox Code Playgroud)

尝试这个:Default Credentials are used,using Windows Authentication

ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Run Code Online (Sandbox Code Playgroud)

//需要更新此 URL 以匹配环境的服务器名称和组织。

Uri OrganizationUri = new Uri("http://XXXXX/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {          
   IOrganizationService service = (IOrganizationService)serviceProxy;
  }
Run Code Online (Sandbox Code Playgroud)