CRM 2011 使用 ADFS 和 HTTPS 进行设置。我正在尝试从自定义网页连接到 organization.svc,该网页与 CRM 2011 位于同一 IIS 上,但使用以下代码作为不同的网站:
OrganizationServiceProxy serviceProxy;
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "admin";
clientCredentials.UserName.Password = "pass";
Guid contactId = Guid.Empty;
Uri OrganizationUri = new Uri(String.Format("https://organization.crmdev.com:port/XRMServices/2011/Organization.svc"));
Uri HomeRealmUri = new Uri(String.Format("https://organization.crmdev.com:port/XRMServices/2011/Discovery.svc"));
using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, clientCredentials, null))
{
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
IOrganizationService service = (IOrganizationService)serviceProxy;
Entity contact = new Entity("contact");
contact.Attributes["lastname"] = "oi oi";
contactId = service.Create(contact);
}
它返回错误消息:
从另一方收到不安全或不正确安全的故障。有关故障代码和详细信息,请参见内部 FaultException。ID3242:无法对安全令牌进行身份验证或授权。
在事件查看器中我看到错误:
Account For Which Logon Failed:
Security ID: NULL SID
Account Name: admin
Account Domain:
Failure Reason: Unknown user name or bad password.
虽然我给出了正确的用户名和密码..
如果我尝试替换:
using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, clientCredentials, null))
和:
using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealUri, clientCredentials, null))
它返回:
你调用的对象是空的。
因为 serviceProxy 为空。