Skip to content

Commit

Permalink
Merge pull request ZeusAutomacao#436 from robertorp/master
Browse files Browse the repository at this point in the history
Cache de certificado digital alterado para aceitar mais de um, quando…
  • Loading branch information
robertorp committed May 11, 2017
2 parents 408f5e6 + 5a20807 commit 5d5a6c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
24 changes: 18 additions & 6 deletions DFe.Utils/Assinatura/CertificadoDigital.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand All @@ -42,7 +43,7 @@ namespace DFe.Utils.Assinatura
{
public static class CertificadoDigital
{
private static X509Certificate2 _certificado;
private static readonly Dictionary<string, X509Certificate2> CacheCertificado = new Dictionary<string, X509Certificate2>();

#region Métodos privados

Expand Down Expand Up @@ -200,16 +201,27 @@ public static X509Certificate2 ObterCertificado(ConfiguracaoCertificado configur
{
if (!configuracaoCertificado.ManterDadosEmCache)
return ObterDadosCertificado(configuracaoCertificado);
if (_certificado != null)
return _certificado;
_certificado = ObterDadosCertificado(configuracaoCertificado);
return _certificado;

if (!string.IsNullOrEmpty(configuracaoCertificado.CacheId) && CacheCertificado.ContainsKey(configuracaoCertificado.CacheId))
return CacheCertificado[configuracaoCertificado.CacheId];

X509Certificate2 certificado = ObterDadosCertificado(configuracaoCertificado);

var keyCertificado = string.IsNullOrEmpty(configuracaoCertificado.CacheId)
? certificado.SerialNumber
: configuracaoCertificado.CacheId;

configuracaoCertificado.CacheId = keyCertificado;

CacheCertificado.Add(keyCertificado, certificado);

return CacheCertificado[keyCertificado];
}


public static void ClearCache()
{
_certificado = null;
CacheCertificado.Clear();
}
}

Expand Down
15 changes: 15 additions & 0 deletions DFe.Utils/ConfiguracaoCertificado.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class ConfiguracaoCertificado : INotifyPropertyChanged
private string _arquivo;
private string _senha;
private TipoCertificado _tipoCertificado;
private string _cacheId;

/// <summary>
/// Tipo de certificado a ser usado
Expand Down Expand Up @@ -127,6 +128,20 @@ public string Senha
}
}

/// <summary>
/// Id do certificado no cache do Zeus
/// </summary>
public string CacheId
{
get { return _cacheId; }
set
{
if (value == _cacheId) return;
_cacheId = value;
OnPropertyChanged("CacheId");
}
}

/// <summary>
/// Manter/Não manter os dados do certificado em Cache, enquanto a aplicação que consome a biblioteca estiver aberta
/// <para>Manter os dados do certificado em cache, aumentará o desempenho no consumo dos serviços, especialmente para certificados A3</para>
Expand Down
2 changes: 2 additions & 0 deletions NFe.AppTeste/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ private void BtnStatusServico_Click(object sender, RoutedEventArgs e)

//Exemplo com using para chamar o método Dispose da classe.
//Usar dessa forma, especialmente, quando for usar certificado A3 com a senha salva.
// se usar cache você pode por um id no certificado e salvar mais de um certificado digital também na memoria com o zeus
//_configuracoes.CfgServico.Certificado.CacheId = "1";
using (var servicoNFe = new ServicosNFe(_configuracoes.CfgServico))
{
var retornoStatus = servicoNFe.NfeStatusServico();
Expand Down

0 comments on commit 5d5a6c5

Please sign in to comment.