尝试连接到 CRM 2011 Web 服务时出现意外错误。这是背景:
连接字符串(已删除敏感信息):"ServiceUri=https://crmdomain.com/OrgName/XRMServices/2011/Organization.svc; Url=https://crmdomain.com/OrgName; Username= appusername; Password=hidden"/>
按如下方式创建连接:
- 将conn字符串解析成CRMConnection:
var conn = Microsoft.Xrm.Client.CrmConnection.Parse(connString);(此时,CrmConnection对象中的属性看起来正确,包括 ClientCredentials) - 创建组织代理:
var orgProxy = new OrganizationServiceProxy(conn.ServiceUri, conn.HomeRealmUri, conn.ClientCredentials, conn.DeviceCredentials); - 创建数据上下文:
var context = new MyContext(orgProxy);
此时,当从 context 中检索任何数据时,会发生以下 WCF 异常:
发生System.ServiceModel.Security.SecurityNegotiationException Message=调用者未通过服务的身份验证。 来源=mscorlib 堆栈跟踪: 服务器堆栈跟踪: 在 System.ServiceModel.Security.IssuanceTokenProviderBase'1.DoNegotiation(TimeSpan 超时) 在 System.ServiceModel.Security.SspiNegotiationTokenProvider.OnOpen(TimeSpan 超时) 在 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan 超时)
...等等。
InnerException 显示 IsSenderFault=True 和 IsPredefinedFault=True。
这是怎么回事?
请您参考如下方法:
我找到了解决方案。首先请下载 CRM SDK 2011 的 RTW 版本。
连接代码将是:
public static IOrganizationService Service()
{
ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential.UserName ="<username>";
Credentials.Windows.ClientCredential.Password ="<password>";
//This URL needs to be updated to match the servername and Organization for the environment.
Uri OrganizationUri = new Uri("http://<server name>/<organization name>/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
//OrganizationServiceProxy serviceProxy;
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
{
IOrganizationService service = (IOrganizationService)serviceProxy;
return service;
}
}
给你...
干杯!享受编码。
