Transmission with digital certificate c #

1

I'm developing an electronic invoice transmission routine for services in C #.

I have already signed the TAGS according to the city's manual but when I try to do the transmission it returns this error.

<MensagemRetorno>
      <Codigo>E504</Codigo>
      <Mensagem>O certificado digital do prestador de serviços é obrigatório.</Mensagem>
      <Correcao>Envie junto a requisição do serviço o certificado digital do prestador de serviços.</Correcao>
</MensagemRetorno>
    
asked by anonymous 08.08.2016 / 16:23

2 answers

1

Just to help anyone with the same problem I was able to send the certificate with the following code

WsTeste.consulta ws = new WsTeste.consulta();
ws.Url = @"https://wsserver/securemath/math.asmx";
ws.ClientCertificates.Add(X509Certificate.CreateFromCertFile(certPath));
Retorno = ws.consultarLote("teste", XMLAssinado.OuterXml);

For more detail, see this link

    
08.08.2016 / 21:42
0

The problem must be in the string you are sending, it must be badly formatted, try to do the following, first format your string following the documentation of the city hall, it looks more or less as below, and beware, IISretido has to be with S or N and not 0 or 1, pay attention to the documentation for type of Rps, etc.:

char pad = '0';  
string strDeAssinatura = tpRPS.ChaveRPS.InscricaoPrestador.ToString().PadLeft(8, pad) +
                tpRPS.ChaveRPS.SerieRPS.PadRight(5, ' ') +
                tpRPS.ChaveRPS.NumeroRPS.ToString().PadLeft(12, pad) +
                tpRPS.DataEmissao.ToString("yyyyMMdd").PadLeft(8, pad) +
                tpRPS.TributacaoRPS.ToString() +
                tpRPS.StatusRPS.ToString() +
                iisRetido +
                tpRPS.ValorServicos.ToString("N2", _enUS).Replace(".", "").ToString().PadLeft(15, pad) +
                tpRPS.ValorDeducoes.ToString("N2", _enUS).Replace(".", "").ToString().PadLeft(15, pad) +
                tpRPS.CodigoServico.ToString().PadLeft(5, pad) + 
                tomadorTipo +                                              
                tpRPS.CPFCNPJTomador.Item.PadLeft(14, pad);

After signing only this string, you can use the function below for this: Then play the value in tpRPS.Assinatura and you're done. Good luck.

private byte[] Assinatura(string strDeAssinatura)
{   
    KeyInfo keyInfo = new KeyInfo();
    KeyInfoX509Data keyInfoData = new KeyInfoX509Data(_cert);
    RSACryptoServiceProvider rsaprovider = (RSACryptoServiceProvider)_cert.PublicKey.Key;
    keyInfo.AddClause(keyInfoData); 
    RSACryptoServiceProvider rsaKey = _cert.PrivateKey as RSACryptoServiceProvider;
    RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsaKey);
    RSAFormatter.SetHashAlgorithm("SHA1");
    SHA1Managed SHhash = new SHA1Managed();
    byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(new ASCIIEncoding().GetBytes(strDeAssinatura)));   
    return SignedHashValue;
}
    
03.03.2017 / 20:10