Problème de récupération des certificats installé sur les postes clients
Bonjour,
J'ai créé une petite application en ASP.Net 2.0 qui récupère les certificats installé dans le magasin de certificats des clients "répertoire personnel" pour créer avec une signature numérique.
Voici le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace WebApplication4
{
public partial class _Default : System.Web.UI.Page
{
static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static string ToHexString(byte[] bytes)
{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
protected void btnSignature_Click(object sender, EventArgs e)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
int a = certificates.Count;
lbCertCount.Text = System.Convert.ToString(a);
if (a > 0)
{
X509Certificate2 certificate = certificates[1];
string publicKey = certificate.GetPublicKeyString();
lbMypublicKey.Text = publicKey + "<br/>";
// AsymmetricAlgorithm privateKey = certificate.PrivateKey;
RSACryptoServiceProvider privateKey = certificate.PrivateKey as RSACryptoServiceProvider;
// message simple bonjour
byte[] buffer = Encoding.Default.GetBytes("Bonjour");
byte[] signature = privateKey.SignData(buffer, new SHA1Managed());
string me = ToHexString(signature);
lbSignature.Text = me;
RSACryptoServiceProvider publicKey1 = certificate.PublicKey.Key as RSACryptoServiceProvider;
bool verify = publicKey1.VerifyData(buffer, new SHA1Managed(), signature);
if (verify == true)
{
lbControl.Text = "Signature valide";
}
else
{
lbControl.Text = "Signature non Valide";
}
}
}
}
} |
Et voici la page aspx:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSignature" runat="server" Text="Signature"
Height="39px" Width="141px" onclick="btnSignature_Click" /><br />
<label>Nombre de certificates :</label><asp:Label ID="lbCertCount" runat="server"></asp:Label><br />
<label>Ma Clé publique :</label><asp:Label ID="lbMypublicKey" runat="server"></asp:Label>
<br />
<label>Signature :</label><asp:Label ID="lbSignature" runat="server"></asp:Label><br />
<label>Vérification :</label><asp:Label ID="lbControl" runat="server"></asp:Label><br />
</div>
</form>
</body>
</html> |
Mon problème est quand j'exécute l’application dans le mini serveur de MS VS 2010 ca marche tres bien, mais quand je publie l'application dans un environnement de production IIS 6 ça ne marche pas !
NB : j'utilise serveur 2003 + IIS 6 + active directory + serveur de certification locale et mes clients sont des utilisateurs enregistrés dans le domaine DC.
Merci de m'aider.