找不到类型或名称空间名称“ WindowsSecureMimeContext”

时间:2019-04-09 14:40:43

标签: asp.net .net .net-core mailkit mimekit

我正在尝试使用MimeKit发送经过数字签名的电子邮件。我用来签署电子邮件的证书存储在LocalMachine证书存储中,因此我想使用WindowsSecureMimeContext。但是我在尝试使用类型或名称空间时发现错误。我包含了MimeKitMimeKit.Cryptography,但是错误仍然存​​在。如果这有所作为,我将在面向.NET Core 2.2的类库项目中尝试使用它。

这是我的代码,知道为什么我会收到错误消息吗?

using MimeKit;
using MimeKit.Cryptography;
using BO;

namespace BLL
{
    public class EmailService : IEmailService
    {    
        public void Send(Email email)
        {
            CryptographyContext.Register(typeof(WindowsSecureMimeContext));
            var message = new MimeMessage();
            message.Subject = email.Subject;
            using (var ctx = new WindowsSecureMimeContext(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine)) {
            }
        }
    }
}

下面是我能够使用的更新代码。

using MailKit.Net.Smtp;
using Microsoft.Extensions.Configuration;
using MimeKit;
using MimeKit.Cryptography;
using OGC.CIS.ADR.BO;
using System.Security.Cryptography.X509Certificates;

namespace OGC.CIS.ADR.BLL
{
    public class EmailService : IEmailService
    {
        private readonly IConfiguration _config;
        public EmailService(IConfiguration config)
        {
            _config = config;
        }

        public void Send(Email email)
        {
            CryptographyContext.Register(typeof(MySecureMimeContext));
            var message = new MimeMessage();
            var sender = new MailboxAddress(_config.GetValue<string>("AppSettings:SubjectCN"));
            message.From.Add(sender);
            message.Subject = email.Subject;
            var body = new TextPart("plain")
            {
                Text = email.Body
            };
            using (var ctx = new MySecureMimeContext()) {
                var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);
                var certs = store.Certificates.Find(X509FindType.FindBySubjectName, _config.GetValue<string>("AppSettings:SubjectCN"), true);
                //var cert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(certs[0]);
                ctx.Import(certs[0]);
                message.Body = MultipartSigned.Create(ctx, sender, DigestAlgorithm.Sha256, body);
            }
        }
    }
}

这是我创建的用于存储私钥的更新的Import方法。

public void Import (System.Security.Cryptography.X509Certificates.X509Certificate2 certificate)
{
    if (certificate == null)
        throw new ArgumentNullException(nameof(certificate));
    var bcCert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(certificate);
    var fingerprint = bcCert.GetFingerprint();
    if (fingerprints.Add(fingerprint))
        certificates.Add(bcCert);

    if (!keys.ContainsKey(fingerprint))
        keys.Add(fingerprint, Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(certificate.PrivateKey).Private);    
}

1 个答案:

答案 0 :(得分:0)

该库的.NETStandard 2.0版本中不存在WindowsSecureMimeContext,因为该类所需的System.Security API不存在。

您需要编写一个自定义的SecureMimeContext类。如果您只想简单一些,则很可能希望将DefaultSecureMimeContext或TemporarySecureMimeContext子类化。