我需要让我的应用程序的用户能够使用他们的个人 USB 安全令牌签署批准。
我已成功签署数据,但无法获取谁的令牌已用于执行此操作的信息。
这是我到目前为止的代码:
CspParameters csp = new CspParameters(1, "SafeNet RSA CSP");
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
// Create some data to sign.
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
Console.WriteLine("Data : " + BitConverter.ToString(data));
// Sign the data using the Smart Card CryptoGraphic Provider.
byte[] sig = rsa.SignData(data, "SHA1");
Console.WriteLine("Signature : " + BitConverter.ToString(sig));
令牌信息中有一个字段称为“令牌名称”。我如何访问该字段以验证哪个令牌已用于签署批准?
附加信息和更新:
- “令牌名称”始终与所有者的名称匹配(拥有 USB 的用户)
令牌)
- 好像做不到,也许有网络服务或者
我需要打电话才能直接获取信息
来自认证机构。
当我最初问这个问题时,我对数字证书的理解非常基础,所以这个问题没有被正确地提出。现在我明白了,我需要从智能卡设备访问证书、查询其属性并测试用户是否可以为其输入正确的 PIN。
这是我用来执行此操作的代码:
//Prompt the user with the list of certificates on the local store.
//The user have to select the certificate he wants to use for signing.
//Note: All certificates form the USB device are automatically copied to the local store as soon the device is plugged in.
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509CertificateCollection certificates = X509Certificate2UI.SelectFromCollection(store.Certificates,
"Certificados conocidos",
"Por favor seleccione el certificado con el cual desea firmar",
X509SelectionFlag.SingleSelection
);
store.Close();
X509Certificate2 certificate = null;
if (certificates.Count != 0)
{
//The selected certificate
certificate = (X509Certificate2)certificates[0];
}
else
{
//The user didn't select a certificate
return "El usuario canceló la selección de un certificado";
}
//Check certificate's atributes to identify the type of certificate (censored)
if (certificate.Issuer != "CN=............................., OU=................., O=..., C=US")
{
//The selected certificate is not of the needed type
return "El certificado seleccionado no corresponde a un token ...";
}
//Check if the certificate is issued to the current user
if (!certificate.Subject.ToUpper().Contains(("E=" + pUserADLogin + "@censoreddomain.com").ToUpper()))
{
return "El certificado seleccionado no corresponde al usuario actual";
}
//Check if the token is currently plugged in
XmlDocument xmlDoc = new XmlDocument();
XmlElement element = xmlDoc.CreateElement("Content", SignedXml.XmlDsigNamespaceUrl.ToString());
element.InnerText = "comodin";
xmlDoc.AppendChild(element);
SignedXml signedXml = new SignedXml();
try
{
signedXml.SigningKey = certificate.PrivateKey;
}
catch
{
//USB Token is not plugged in
return "El token no se encuentra conectado al equipo";
}
DataObject dataObject = new DataObject();
dataObject.Data = xmlDoc.ChildNodes;
dataObject.Id = "CONTENT";
signedXml.AddObject(dataObject);
Reference reference = new Reference();
reference.Uri = "#CONTENT";
signedXml.AddReference(reference);
//Attempt to sign the data. The user will be prompted to enter his PIN
try
{
signedXml.ComputeSignature();
}
catch
{
//User didn't enter the correct PIN
return "Hubo un error confirmando la identidad del usuario";
}
//The user has signed with the correct token
return String.Format("El usuario {0} ha firmado exitosamente usando el token con serial {1}", pUserADLogin, certificate.SerialNumber);
Sources:
http://stormimon.developpez.com/dotnet/signature-electronique/ http://stormimon.developpez.com/dotnet/signature-electronique/(法语)https://www.simple-talk.com/content/print.aspx?article=1713 https://www.simple-talk.com/content/print.aspx?article=1713(用英语讲)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)