C# – CRM 2011 SecurityNegotiationException trying to access web services

cdynamics-crmdynamics-crm-2011

Getting an unexpected error when trying to connect to CRM 2011 web service. Here's the background:

Connection String (with sensitive info removed): "ServiceUri=https://crmdomain.com/OrgName/XRMServices/2011/Organization.svc; Url=https://crmdomain.com/OrgName; Username=appusername; Password=hidden"/>

Creating the connection as follows:

  1. Parse conn string into CRMConnection: var conn = Microsoft.Xrm.Client.CrmConnection.Parse(connString); (at this point, the properties in the CrmConnection object look correct, including ClientCredentials)
  2. Create org proxy: var orgProxy = new OrganizationServiceProxy(conn.ServiceUri, conn.HomeRealmUri, conn.ClientCredentials, conn.DeviceCredentials);
  3. Create data context: var context = new MyContext(orgProxy);

At this point, when retrieving any data from context, the following WCF exception occurs:

System.ServiceModel.Security.SecurityNegotiationException occurred
Message=The caller was not authenticated by the service.
Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Security.IssuanceTokenProviderBase'1.DoNegotiation(TimeSpan timeout)
at System.ServiceModel.Security.SspiNegotiationTokenProvider.OnOpen(TimeSpan timeout)
at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)

… and so on.

The InnerException shows IsSenderFault=True and IsPredefinedFault=True.

What's going on here?

Best Answer

I found the solution. First of all please download the RTW release of CRM SDK 2011.

Code for connectivity will be :

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

and here you go...

Cheers! Enjoy coding.

Related Topic