Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Access to Microsoft CRM Dynamics 365 from .Net

Posted on by 25

I need access to CRM Dynamics 365 i use below code to access its worked fine with "ADFS (IFD) Authentication", but with CRM Online "Dynamics 365 https://OrgName.api.crm4.dynamics.com/XRMServices/2011/Organization.svc"

getting this Error:

An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   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 System.ServiceModel.Security.IWSTrustContract.Issue(Message message)
   at System.ServiceModel.Security.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.AuthenticateWithOrgIdForACS(AuthenticationCredentials authenticationCredentials, Uri identifier)
   at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateOnlineFederationInternal(AuthenticationCredentials authenticationCredentials)
   at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.Authenticate(AuthenticationCredentials authenticationCredentials)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration.Authenticate(AuthenticationCredentials authenticationCredentials)

This my code:

 

public static IOrganizationService GetService()
{

    try
    {
        OrganizationServiceProxy _serviceProxy;

        string userName = "";
        string password = "";
        string domain = "";
        Uri serviceUri;


        serviceUri = new Uri(ConfigurationManager.AppSettings["URL"]);
        userName = ConfigurationManager.AppSettings["UserName"];
        password = ConfigurationManager.AppSettings["Password"];
        domain = ConfigurationManager.AppSettings["domain"];


        IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(serviceUri);

        if (orgServiceManagement.AuthenticationType == AuthenticationProviderType.Federation)
        {
            // ADFS (IFD) Authentication
            ClientCredentials clientCred = new ClientCredentials();
            clientCred.UserName.UserName = userName;
            clientCred.UserName.Password = password;
            _serviceProxy = new OrganizationServiceProxy(orgServiceManagement, clientCred);
        }
        else if (orgServiceManagement.AuthenticationType == AuthenticationProviderType.OnlineFederation)
        {
            // Online authentication
            AuthenticationCredentials authCredentials = new AuthenticationCredentials();

            authCredentials.ClientCredentials.UserName.UserName = userName;
            authCredentials.ClientCredentials.UserName.Password = password;

            // Error getting here
            AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials); 


            _serviceProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);
        }
        else
        {
            // On-Premise, non-IFD authentication
            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = new NetworkCredential(userName, password, domain);
            _serviceProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
        }

        _serviceProxy.EnableProxyTypes();


    }
    catch (Exception e)
    {

    }

    return (IOrganizationService)_serviceProxy;
}

*This post is locked for comments

  • Mahmoud Fahmy Profile Picture
    Mahmoud Fahmy 25 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Thank you all for your effort to help me :)

  • Mahmoud Fahmy Profile Picture
    Mahmoud Fahmy 25 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    @Nithya Gopinath Thank you so much for this post, its very simple & finally my code and connection worked fine, really thank you so much my friend :)

  • Suggested answer
    ansrikanth Profile Picture
    ansrikanth 3,115 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Are you sure that you are using correct O365 connection string :

    <add name="MyCRMServer" connectionString="AuthType=Office365;Username=jsmith@contoso.onmicrosoft.com; Password=passcode;Url=https://contoso.crm.dynamics.com&quot;/>  

    should be the connection string you should use (refer : msdn.microsoft.com/.../mt608573.aspx) for more options like using UI prompt to enter cred etc.

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Hi,

    Please use the code in the following blog. It is very simple to use.

    arunpotti.wordpress.com/.../connect-to-crm-online-or-on-premise-using-c

  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Can you share the connection code you used?

  • Mahmoud Fahmy Profile Picture
    Mahmoud Fahmy 25 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Thank you for your answer, i used simplified connection way but i getting the below exception.

    "Unable to Login to Dynamics CRMOrganizationServiceProxy is null"

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Access to Microsoft CRM Dynamics 365 from .Net

    Hi,

    You should use simplified connection, it's much easier and you will save a lot of time if you will have mutliple environments you want to connect to:

    msdn.microsoft.com/.../jj602970.aspx

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans