locked
does the old style ServiceConnection work for 2013 online RRS feed

  • Question

  • Is this because I'm not using the new style with the machine identification for the ServiceModel?

    I created a trial 2013 instance under my name and started a VS WCF Web Service project.  I have no problem with my credentials using the old:

        public class ServiceConnection {
            private static IOrganizationService _service;
            private static OrganizationServiceProxy _serviceProxy;
            private static OrganizationServiceContext orgContext;
    
            public static IOrganizationService GetService() {
                if (_service == null) {
                    Connect();
                }
    
                return _service;
            }
    
            private static void Connect() {
                string serverURL = ConfigurationManager.AppSettings["CRMOrgUrl"].ToString();
                string UserName = ConfigurationManager.AppSettings["UserName"].ToString();
                string Password = ConfigurationManager.AppSettings["Password"].ToString();
    
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
    
                Uri serviceUri = new Uri(serverURL);
    
                _serviceProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                _serviceProxy.EnableProxyTypes();
                _service = (IOrganizationService)_serviceProxy;
                orgContext = new OrganizationServiceContext(_serviceProxy);
            }
        }
    

    Then I set up a new user through the O365 portal for crm admin.  I can access CRM via the browser fine with his credentials.  However, I get a System.ServiceModel.Security.MessageSecurityException from the web service using his credentials.

    System.ServiceModel.Security.MessageSecurityException was unhandled by user code
      HResult=-2146233087
      Message=An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
      Source=mscorlib
      StackTrace:
        Server stack trace: 
           at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
           at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
           at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
           at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
           at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
        Exception rethrown at [0]: 
           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
           at Microsoft.IdentityModel.Protocols.WSTrust.IWSTrustContract.Issue(Message message)
           at Microsoft.IdentityModel.Protocols.WSTrust.WSTrustChannel.Issue(RequestSecurityToken rst, RequestSecurityTokenResponse& rstr)
           at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.Issue(AuthenticationCredentials authenticationCredentials)
           at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateInternal(AuthenticationCredentials authenticationCredentials)
           at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateCrossRealm(ClientCredentials clientCredentials, String appliesTo, Uri crossRealmSts)
           at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration.AuthenticateCrossRealm(ClientCredentials clientCredentials, String appliesTo, Uri crossRealmSts)
           at Microsoft.Xrm.Sdk.Client.ServiceProxy`1.AuthenticateOnlineFederation()
           at Microsoft.Xrm.Sdk.Client.ServiceProxy`1.AuthenticateCore()
           at Microsoft.Xrm.Sdk.Client.ServiceProxy`1.Authenticate()
           at Microsoft.Xrm.Sdk.Client.ServiceProxy`1.ValidateAuthentication()
           at Microsoft.Xrm.Sdk.Client.ServiceProxy`1.get_ServiceChannel()
           at Microsoft.Xrm.Sdk.Client.ServiceContextInitializer`1.Initialize(ServiceProxy`1 proxy)
           at Microsoft.Xrm.Sdk.Client.ServiceContextInitializer`1..ctor(ServiceProxy`1 proxy)
           at Microsoft.Xrm.Sdk.Client.OrganizationServiceContextInitializer..ctor(OrganizationServiceProxy proxy)
           at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.RetrieveMultipleCore(QueryBase query)
           at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.RetrieveMultiple(QueryBase query)
    

    Just thought I'd ask before going through the whole process of replacing the ServiceConnection code.

    Monday, October 6, 2014 3:27 PM

All replies

  • Hello,

    Your code would work for CRM Onprem but for CRM Online it would not. For CRM Online you should use following code:

    IServiceManagement<IOrganizationService> orgServiceManagement =
     ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://orgname.api.crm.dynamics.com/XRMServices/2011/Organization.svc"));
     
    AuthenticationCredentials authCredentials = new AuthenticationCredentials();
    authCredentials.ClientCredentials.UserName.UserName = _userName;
    authCredentials.ClientCredentials.UserName.Password = _password;
    AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
     
    IOrganizationService service = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Tuesday, October 7, 2014 6:52 PM
    Moderator
  • or use a simplified connection:

    CrmConnection crmConnection = CrmConnection.Parse("Url=https://XXX.crm.dynamics.com; Username=user@domain.onmicrosoft.com; Password=passwordhere;");
    OrganizationService service = new OrganizationService(crmConnection);


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, October 7, 2014 7:06 PM