401 de error llamando servicio web CRM4
-
21-08-2019 - |
Pregunta
Tengo este código que funciona en una prueba de unidad, pero no funciona cuando se ejecuta en el contexto de un plugin. Lo que el código hace es tratar de crear una ventaja mediante una llamada al servicio web CRM4.
Cuando se ejecuta el plugin consigo la siguiente excepción: "estado HTTP 401: no autorizado"
Este es el código que inicializa una instancia de servicio web
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = GetConfig("crm.organisation_name");
_crmService = new CrmService(GetConfig("webservice.crm"));
_crmService.CrmAuthenticationTokenValue = token;
_crmService.UseDefaultCredentials = false;
_crmService.PreAuthenticate = false;
_crmService.Credentials = new NetworkCredential(GetConfig("crm.user_username"),
GetConfig("crm.user_password"),
GetConfig("crm.user_domain"));
Alguien tiene consejos sobre lo que puedo probar la próxima? Se crea el plomo cuando la prueba se ejecuta, y la información de configuración es la misma en la prueba de la unidad, ya que es cuando la aplicación está ejecutando el plugin.
Solución
En lugar de instanciar la CrmService por sí mismo, también, puede obtener el CrmService mediante la obtención de la referencia de la IPluginExecutionContext e invocar el método CreateCrmService
Por favor, consulte esta enlace con respecto a la creación de la CrmService de IPluginExecutionContext
Here is some code snippet
public void Execute(IPluginExecutionContext context)
{
// the below code means, the CrmService will be created
// by referring to the user account who is registered to
// run the CRM Application Pool
ICrmService crmService = context.CreateCrmService(false);
// the below code means, the CrmService will be created
// by taking account the user account who login and run
// the current plugin
ICrmService crmService = context.CreateCrmService(true);
// the below code means, the CrmService will be created
// by impersonating a valid user
ICrmService crmService = context.CreateCrmService(new Guid("3F2504E0-4F89-11D3-9A0C-0305E82C3301"));
}
Saludos,
Hadi