title: CRM 2011 connect to organization.svc using C#
body: <p>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:</p>
  
  <pre class="lang-cs prettyprint-override"><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);
  }
  </code></pre>
  
  <p>It returns error message:</p>
  
  <p>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. </p>
  
  <p>and in the event viewer I see error:</p>
  
  <pre><code>Account For Which Logon Failed:
      Security ID:        NULL SID
      Account Name:       admin
      Account Domain: 
  Failure Reason:     Unknown user name or bad password.
  </code></pre>
  
  <p>although I give the correct user name and password..</p>
  
  <p>and if I try to replace:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, clientCredentials, null))
  </code></pre>
  
  <p>with:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealUri, clientCredentials, null))
  </code></pre>
  
  <p>it returns:</p>
  
  <p>Object reference not set to an instance of an object.</p>
  
  <p>because serviceProxy is null.</p>