web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Call Web API service from Plugin

(0) ShareShare
ReportReport
Posted on by 5

Dear members,

At the moment I am having a System.Security.Permission problem (Request Failed message). I want to send a web API service message via a plugin. This must be done via an HttpRequest object because an SSL connection has to be set up. The plugin is executed in a sandbox.
In the HttpRequest object, which is an implementation of a WebClient object, a certificate is also included.

Code snippet:

internal class EngieApiWebClient : WebClient
{
private readonly IOrganizationService _organizationservice;

public ApiWebClient(IOrganizationService service)
{
_organizationservice = service;
}

protected override WebRequest GetWebRequest(Uri address)
{
// Get the request...
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
// ...and add the certificate

AuthCertificate.GetCertificate(_organizationservice);
request.ClientCertificates.Add(AuthCertificate.Certificate);
// Return the request
return request;
}

}

The call in the plugin is:

using (var wc = new ApiWebClient(CrmOrganisationService))
{
     try
     {
          var data = wc.DownloadString($"{WebApiUrl}{SearchMethod}/{Postalcode}/{Housenumber}");
          if (data.Length > 0) {
                   ConnectionResult connections = JsonConvert.DeserializeObject<ConnectionResult>(data);
            if (connections.IsSuccess)
               {
                  connections.Result.ForEach(c =>
                     {
                         CreateConnectionInCRM(c);
                     }
                  });
           }
        }
        catch (WebException we)
        { }
    }
}

ConnectionResult is a Model Class.

What should i do to solve the problem so the plugin will execute normally without permission exceptions.

Regards,

Hans

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Could you please add Security Protocol in your code  -

    public static OrganizationServiceProxy GetOrganizationServiceProxy()
    {
    ClientCredentials clientCredentials = new ClientCredentials();
     
    clientCredentials.UserName.UserName = "username";
    clientCredentials.UserName.Password = "password";
     
    // Set security protocol to TLS 1.2 for version 9.0 of Customer Engagement Platform
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
     
    return new OrganizationServiceProxy(new Uri("orgname.crm.dynamics.com/.../Organization.svc"),
    null, clientCredentials, null

    }

  • Hans Heeren Profile Picture
    5 on at

    The security protocol has no effect,

    I do receive the error : System.Security.SecurityException: Request Failed when i want to download data from an API webservice .

    In the WebClient i added a certificate which is accepted by the customer who holds the API webservice, If I execute the plugin in the debugger all is worked fine and as expected. (In isolated mode "Sandbox" with the plugin registration tool)

    The plugin executing is failed in a portal which is build from CRM online.  

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Could you please share you complete code? How are you referencing the certificate, is it stored on your local drive?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans