skip to Main Content

When I used the authtype clientsecret for the console application, it worked, however for the web application, (authtyp) AD was changed automatically insted of clientSecret and it is not working. Please help how can i achieve on asp.net mvc web application.

Note : The same code runs successfully on a console program but fails
on a web application.

Please find my code below.

Connection String :

  <connectionStrings>
    <add name="MyCrm1" connectionString="authtype=ClientSecret;url=https://fake.crm11.dynamics.com/XRMServices/2011/Organization.svc;clientid=fd0a8505-e559-4145-b1c8-929d0c9e0ce1;ClientSecret=CVm8Q~hfG0K1Itm-CSGWk1dOtO54T2ghPHmlNc7K;LoginPrompt=False;redirectUri=app://fd0a8505-e559-4145-b1c8-929d0c9e0ce1;"/>
  </connectionStrings>

ASP.net MVC C#

 private static IOrganizationService _crmServiceClient()
        {
            using (var connection = new CrmServiceClient(_connectionString))
            {
                if (connection.IsReady)
                {
                     _service = (IOrganizationService)connection.OrganizationWebProxyClient != null ? (IOrganizationService)connection.OrganizationWebProxyClient : (IOrganizationService)connection.OrganizationServiceProxy;

                }
            }

            return _service;

        }

Connection is null on CRMServiceClient

2

Answers


  1. Can I ask you why do you need this helper code:

    _service = (IOrganizationService)connection.OrganizationWebProxyClient != null ? (IOrganizationService)connection.OrganizationWebProxyClient : (IOrganizationService)connection.OrganizationServiceProxy;
    

    Your connection implements IOrganizationService interface so can be perfectly used for any kind of CRUD and Execute operations.

    Login or Signup to reply.
  2. Check Redirect URI: Ensure that the redirectUri in your connection string is set to a valid value. For web applications, this should typically be set to the URL of your web application.

    Authentication Flow: In a web application, the authentication flow can differ from a console application. Make sure that you are not inadvertently triggering a different authentication flow. For instance, if you have other authentication middleware (e.g., Azure AD) in your ASP.NET MVC application, it might interfere with the CRM authentication.

    Try Different Approaches: You can also try different approaches for connecting to CRM in a web application. For example, you can use ADAL (Azure Active Directory Authentication Library) or MSAL (Microsoft Authentication Library) for acquiring the token and then use that token to authenticate with Dynamics CRM.

    Certainly, here’s a sample code snippet for connecting to Dynamics CRM using a Client Secret in an ASP.NET MVC web application. This code assumes you’ve already set up your Azure AD App Registration and have the necessary connection string in your web.config file.

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Tooling.Connector;
    using System;
    
    namespace YourMVCApp.Controllers
    {
        public class DynamicsCRMController : Controller
        {
            // Define your connection string (from web.config or other configuration method)
            private string _connectionString = ConfigurationManager.ConnectionStrings["MyCrm1"].ConnectionString;
    
            // This method establishes a connection to Dynamics CRM using Client Secret authentication
            private IOrganizationService ConnectToDynamicsCRM()
            {
                CrmServiceClient service = new CrmServiceClient(_connectionString);
    
                if (service.IsReady)
                {
                    return (IOrganizationService)service.OrganizationWebProxyClient ?? (IOrganizationService)service.OrganizationServiceProxy;
                }
                else
                {
                    throw new Exception("Failed to connect to Dynamics CRM.");
                }
            }
    
            public ActionResult Index()
            {
                try
                {
                    // Connect to Dynamics CRM
                    IOrganizationService orgService = ConnectToDynamicsCRM();
    
                    // Now you can use orgService to interact with Dynamics CRM
                    // For example, you can retrieve data or perform operations on CRM records
    
                    // Sample: Retrieve the first account record
                    var query = new QueryExpression("account")
                    {
                        ColumnSet = new ColumnSet("name"),
                        PageInfo = new PagingInfo { Count = 1 }
                    };
    
                    EntityCollection results = orgService.RetrieveMultiple(query);
    
                    if (results.Entities.Count > 0)
                    {
                        string accountName = results.Entities[0].GetAttributeValue<string>("name");
                        ViewBag.AccountName = accountName;
                    }
                    else
                    {
                        ViewBag.AccountName = "No accounts found.";
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.ErrorMessage = "Error connecting to Dynamics CRM: " + ex.Message;
                }
    
                return View();
            }
        }
    }
    

    This code includes a controller action (Index) that connects to Dynamics CRM using the provided Client Secret-based connection string and retrieves the name of the first account record. You can modify and expand this code to suit your specific requirements within your ASP.NET MVC web application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search