Transmission with digital certificate c#

Asked

Viewed 683 times

1

I am developing a routine of transmitting electronic invoice services in c#.

I’ve already signed the TAGS according to the city manual but when I try to make the transmission 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>

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 details see this link

0

The problem should be in the string you are sending, which should be badly formatted, try to do the following, first format your string following the documentation of the prefecture, gets more or less as below, and beware, iisretido has to be with S or N and not 0 or 1, if you look at 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);

Then just sign this string, you can use the function below to do this: Then play the value in tpRPS.Subscription and ready. 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.