locked
Embed certificate in plugin RRS feed

  • Question

  • I'm trying to include an x509 certificate which I need for talking to an external API. I'm not sure if this is the correct/best way or whether it's even possible.

    I'm getting the error "Object reference not set to an instance of an object."

    I have the pfx in my solution with the Build Action set to 'Embedded Resource'

    var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("key.pfx");
    tracer.Trace("load Cert");
    var bytes = new byte[stream.Length]; //this line gives the error
    tracer.Trace("Read Cert");
    stream.Read(bytes, 0, bytes.Length);
    tracer.Trace("Cert read");
    var cert = new X509Certificate2(bytes,"password");

    Any help or pointers where I'm going wrong will be much appreciated.

    Monday, July 20, 2015 3:40 PM

Answers

  • I don't think you'll be able to use certificates in Crm Online. One option would be:

    • Create a separate web service and deploy it to the Azure Service Bus
    • Within this web service, call the external API with the certificate
    • Call the web service from your plugin, passing the execution context from Crm

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, July 21, 2015 9:47 PM
    Moderator

All replies

  • Hello,

    It seems the method GetManifestResourceStream returns null in your case.

    I recommend you add a Resource file to your project (.resx). In this resource file add the pfx file.

    You will be able to access the pfx file by typing ResourceFileName.PfxFileName that will return a byte array containing the certificate.

    You would just have to do :

    var bytes = ResourceFileName.PfxFileName;
    var cert = new X509Certificate2(bytes, "password");

    Monday, July 20, 2015 10:50 PM
  • Hello,

    Many thanks for your reply. That fixed the error I was getting on that line.

    However, because I'm using CRM Online I'm getting an error that System.Web.AspNetHostingPermission isn't available. Is there another assembly I can use that is trusted by CRM Online?

    Thanks,

    Chris

    Tuesday, July 21, 2015 3:08 PM
  • I don't think you'll be able to use certificates in Crm Online. One option would be:

    • Create a separate web service and deploy it to the Azure Service Bus
    • Within this web service, call the external API with the certificate
    • Call the web service from your plugin, passing the execution context from Crm

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, July 21, 2015 9:47 PM
    Moderator
  • I'm looking at the Azure options now. I'd like to check I going about this in the correct way.

    From CRM, I'll send messages to a queue or topic -> subscription ready to be processed.

    I'll have a Web Role running which reads the information from the queue/subscription and handles the communication with the external API and posts back to the response queue.

    The CRM accesses the response queue to update the relevant record. Should the CRM wait synchronously for this, or periodically poll the response queue?

    • Edited by ChrisJC119 Tuesday, September 15, 2015 11:58 AM change of question
    Monday, September 14, 2015 4:23 PM