C# 连接到CRM

C# 连接到CRM,c#,asp.net,dotnetnuke,crm,web-config,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>";

我需要我的网站连接到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");
        }
var organizationUri=新Uri(ConfigurationManager.AppSettings[“CRMServerUrl]”);
//客户端凭据
var credentials=new ClientCredentials();
credentials.UserName.UserName=@;
credentials.UserName.Password=“”;
//使用名为“CrmConnectionStr”的web.config文件中的Microsoft Dynamics CRM联机连接字符串。
使用(OrganizationServiceProxy _service=new OrganizationServiceProxy(organizationUri,null,凭据,null))
{
回复。书面(“关联”);
}
这在我的web.config中

 <add key="CRMServerUrl" value="http://XXXXX/XRMServices/2011/Organization.svc" />
<add key="username" value="<user>" />
<add key="password" value="<password>" />

它向我提供了以下错误消息:

“发生严重错误。无法连接到CRM服务器。服务器 服务未对调用方进行身份验证。“


通过查看您的代码,您没有使用用户名和密码
更改以下行:

    //Client credentials
    var credentials = new ClientCredentials();
    credentials.UserName.UserName =ConfigurationManager.AppSettings["username"].toString();
    credentials.UserName.Password =ConfigurationManager.AppSettings["password"].toString();
尝试此操作:
使用Windows身份验证使用默认凭据

ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
//需要更新此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;
  }
您应该使用类型提供的,以获得更轻松的体验

步骤很简单:

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

<connectionStrings>
  <add name="CrmConnStr" connectionString="!SEE EBELOW!"/>
</connectionStrings>
关于连接字符串,如果本地没有IFD,则应

"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
<!-- https is always nice, but it's not mandatory in this case -->
"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
<!-- https is of course mandatory here -->
<!-- 'DOMAIN=...' part is not needed because of ADFS -->
“Url=http[s]://服务器Url/组织;域=域;用户名=用户名;密码=密码”
如果在IFD内部或在线,则应

"Url=http[s]://serverurl/organization; Domain=DOMAIN; Username=USERNAME; Password=PASSWORD"
<!-- https is always nice, but it's not mandatory in this case -->
"Url=https://org.server.url; Username=USERNAME; Password=PASSWORD"
<!-- https is of course mandatory here -->
<!-- 'DOMAIN=...' part is not needed because of ADFS -->
“Url=https://org.server.url;用户名=用户名;密码=密码”

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

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client.Services;
创建组织服务:

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

不要忘记导入
System.Runtime.serialization

未指定组织名称可能会导致身份验证不正确。。应该是这样的:


使用Microsoft.Xrm.Tooling.Connector

    var service =new CrmServiceClient(
                "AuthType=Office365;Username=username; Password=password;Url=https://url");

@确保用户名和密码正确无误。您可以使用Windows身份验证您有什么样的部署?它是基于Prem、IFD还是在线的CRM?您正在使用的服务的完整url是什么?您正在指定组织名称吗?这可能是身份验证不正确的问题。。它应该是这样的:我遵循您的代码,它可以工作,但它给出了一个错误:“调用方未通过服务验证。”@Dania您可以从浏览器访问CRM吗?您是否将“我的代码”中的URL更新为Your?我认为示例连接字符串开头缺少
URL=
。这是最好的答案。客户端被Xrm.Tooling替换。如果您要连接到在线实例,请确保使用此方法连接到Dynamics 365租户时不需要使用付费Dynamics 365用户许可证。