Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Erro de Plugin - Certificate

Posted on by 35
Good morning,

Developing the code in C # and to call this code from Dynamics, performing the creation of a plugin and I called the same via JS, however it presents me an error.

error = {errorCode: 2147746340, message: "Unexpected error in ISV code.", code: 2147746340, title: "The specified domain does not exist or cannot be contacted", raw: "{" _errorCode ": 2147746340," _ errorFault ": {" _ reply… textId ":" eb7e193a-f37c-419f-a37f-971ec0d9e9d3 "}}}"}

From the tests I did I believe it is a part referring to the X509 certificate.



using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using IntegraçãoTeste.psp;

using System.Net;
using System.Xml;
using System.Security.Cryptography.Xml;
using System.IO;

namespace IntegraçãoTeste
{
    public class CustomAction : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            XmlDocument doc = new XmlDocument();
            string req = "    9999999999    9999999999 ";
            doc.LoadXml(req);


            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, "Nome Empresa:9999999999", true);
            var cert1 = certificates[0];

            AssinaDocComCertificado(doc, cert1);

            StringWriter sw = new StringWriter();
            XmlTextWriter tx = new XmlTextWriter(sw);
            doc.WriteTo(tx);

            string str = sw.ToString();

            br.gov.sp.prefeitura.nfe.LoteNFe ws = new br.gov.sp.prefeitura.nfe.LoteNFe();
            ws.ClientCertificates.Add(cert1);
            Console.WriteLine(ws.ConsultaCNPJ(1, str));
            
            // teste
            Entity contact = new Entity("contact");
            contact["firstname"] = "Bob";
            contact["lastname"] = "Smith";
            contact["description"] = "funcionou";
            Guid contactId = service.Create(contact);
            
        }

        public static void AssinaDocComCertificado(XmlDocument doc, X509Certificate2 cert1)
        {
            SignedXml signedXml = new SignedXml(doc);
            signedXml.SigningKey = cert1.PrivateKey;
            Reference reference = new Reference();
            reference.Uri = "";
            reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            signedXml.AddReference(reference);

            KeyInfo keyinfo = new KeyInfo();
            keyinfo.AddClause(new KeyInfoX509Data(cert1));

            signedXml.KeyInfo = keyinfo;
            signedXml.ComputeSignature();
            XmlElement xmlSig = signedXml.GetXml();

            doc.DocumentElement.AppendChild(doc.ImportNode(xmlSig, true));

        }
    }
}


If anyone can help me.


Grateful,

Felipe
  • Felipe Kavai Profile Picture
    Felipe Kavai 35 on at
    RE: Erro de Plugin - Certificate

    Hi,

    Sorry to bother you again, I'm still having a lot of doubts about how to build an API that searches for the certificate on my local computer, or there is only some way that I upload the file in .pfx for later consultation.

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
  • Felipe Kavai Profile Picture
    Felipe Kavai 35 on at
    RE: Erro de Plugin - Certificate

    Hi,

    Thank you very much for the explanation, but I am new to developing plugins, could you help me with any example or documentation related to the subject?

    Grateful,

    Philip

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: Erro de Plugin - Certificate

    Hi,

    Thank you for your query?

    Is it online version? If yes, you cannot use third-party libraries in the online version.

    Instead you should call an API (with named-url) to the heavy lifting. This means, use this intermediate API to do the operation (using third party lib) and return back the results to plugin.

    Another way is to use ILMerge (which merges 3rd-party libs with your plugin dll), but it is not a supported way.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans