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;
}
2
Answers
Can I ask you why do you need this helper code:
Your connection implements IOrganizationService interface so can be perfectly used for any kind of CRUD and Execute operations.
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.
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.