【问题标题】:CRM 2011 connect to organization.svc using C#CRM 2011 使用 C# 连接到 organization.svc
【发布时间】:2013-03-28 01:01:36
【问题描述】:

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 为空。

【问题讨论】:

    标签: service dynamics-crm-2011 credentials discovery


    【解决方案1】:

    所以,我自己刚刚开始使用 ADFS,如果您还没有,我建议您阅读 Active Directory and Claims-Based Authentication

    另外,通过查看您的代码,我认为您的 HomeRealmUri 不正确。您似乎已经给它提供了 CRM 发现服务的地址。如果您只有一个 ADFS 在玩,我认为您可以将其保留为空。如the MSDN here.中所述

    我本来希望它看起来更像这样:urn:federation:contoso

    对于用户名我相信你需要指定域,你通常必须这样做格式:username@domain

    您可能还想查看此example,它是一个与 Crm 对话的网页上的单一登录,这听起来很像您想要实现的目标。

    祝你好运。

    【讨论】:

    • 它帮助将用户名写为“username@domain”并且它确实验证了用户:) 但是现在当我尝试使用服务代理检索某些实体时出现新的异常:访问被拒绝:(
    • 该用户在 CRM 中有权限吗?
    • 我在 clientCredentials.UserName.UserName 中输入的用户凭据是 CRM 的管理员帐户
    • 没有想法,恐怕
    • 谢谢詹姆斯,你的回答帮了我一半:)
    【解决方案2】:

    我终于明白了:)

    我错过了“/organization”,这意味着 organizationUri 应该是:

     Uri OrganizationUri = new Uri(String.Format("https://organization.crmdev.com:port/organization/XRMServices/2011/Organization.svc"));
    

    当我使用这段代码时我已经想通了:

    IDiscoveryService discoveryService = new DiscoveryServiceProxy(discoveryUri, null, userCredentials, null);
    
                RetrieveOrganizationRequest orgRequest =
                                            new RetrieveOrganizationRequest()
                                            {
                                                UniqueName = "organization name",
                                                AccessType = EndpointAccessType.Default,
                                                Release = OrganizationRelease.Current
                                            };
                RetrieveOrganizationResponse org =
                        (RetrieveOrganizationResponse)discoveryService.Execute(orgRequest);
    

    并看到组织具有的端点包括 Uri 中的“/organization”..

    【讨论】:

    • 它是如此的琐碎和愚蠢,我敢打赌你会觉得自己真的很愚蠢,以至于你被困了好几天......;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多