Search code examples
servicedynamics-crm-2011credentialsdiscovery

CRM 2011 connect to organization.svc using C#


The CRM 2011 is setup with ADFS and HTTPS. I'm trying to connect to organization.svc from custom web page which sits on the same IIS with CRM 2011 but as a different web site using this code:

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

It returns error message:

An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.ID3242: The security token could not be authenticated or authorized.

and in the event viewer I see error:

Account For Which Logon Failed:
    Security ID:        NULL SID
    Account Name:       admin
    Account Domain: 
Failure Reason:     Unknown user name or bad password.

although I give the correct user name and password..

and if I try to replace:

using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, clientCredentials, null))

with:

using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealUri, clientCredentials, null))

it returns:

Object reference not set to an instance of an object.

because serviceProxy is null.


Solution

  • I've finally figured it out :)

    I was missing the "/organization", meaning the organizationUri should be:

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

    I've figured it out when I used this code:

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

    And saw the endpoins that the organization have include the "/organization" in the Uri..