简体   繁体   English

.NetCore 用证书签名电子邮件

[英].NetCore Signing Emails with Certificate

First of all, I tried FluentEmail but that one seems to be broken with my version of .NetCore.首先,我尝试了 FluentEmail,但我的 .NetCore 版本似乎损坏了它。

I am creating an email like this:我正在创建这样的电子邮件:

var email = new MimeMessage();

// Put all the data in the email

X509Certificate2 cert = new X509Certificate2("fileName");

The problem is, I can't find any example of how to actually perform the signing.问题是,我找不到任何有关如何实际执行签名的示例。 The best I can find is signing and encrypting but the email MUST NOT be encrypted.我能找到的最好的方法是签名和加密,但电子邮件不得加密。 That one is really important.那一个真的很重要。

Does anyone now how to sign an email (including sender, subject, body, etc.).现在有人知道如何签署电子邮件(包括发件人、主题、正文等)。 Any help would be very much appreciated.任何帮助将不胜感激。

I probably should add that the certificate will be located at some file path and that the program will be deployed on a Linux Docker container.我可能应该补充一点,证书将位于某个文件路径中,并且该程序将部署在 Linux Docker 容器上。 So the code must not only work on Windows.所以代码不能只在 Windows 上工作。

And sorry, should I have overlocked the obvious.对不起,我应该把显而易见的事情包起来。 This is the first time I am trying to send an email via code.这是我第一次尝试通过代码发送电子邮件。

Update 0: Note quite sure if this works because of WindowsSecureMimeContext(..) .更新 0:请注意非常确定这是否有效,因为WindowsSecureMimeContext(..) (I am aware that I only sign the body in the code snipped below). (我知道我只在下面剪下的代码中签署了正文)。

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new WindowsSecureMimeContext(StoreLocation.LocalMachine))
{
   email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

Update 1: I think this should work:更新 1:我认为这应该有效:

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new DefaultSecureMimeContext())
{
   email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

I think this should be the answer to my question (the example only signs the body):我认为这应该是我问题的答案(该示例仅在正文上签名):

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new DefaultSecureMimeContext())
{
  email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

I still need to do some work before I will be able to test this.在我能够测试之前,我仍然需要做一些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM