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