C# | jstedfast/MimeKit |带有 DKIM 设置的 Office 365 连接器

C# | jstedfast/MimeKit | Office 365 connector with DKIM setup

DKIM 是为 Office365 中的域设置的。 .Net 应用程序(当前为 MVC 4)通过 O365 连接器向外部各方发送电子邮件。 我们也想使用 DKIM 对这些进行签名。

我不是很清楚整个过程。 MimeKit's Documentation 相当清楚。我想我可以使用任何 pub/priv 密钥生成器(例如 Putty)来生成密钥对吗?然后我会以 C# 应用程序可以将其读入

的方式存储私钥
var signer = new DkimSigner ("privatekey.pem") {
   SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
   AgentOrUserIdentifier = "@eng.example.com",
   QueryMethod = "dns/txt",
};

public 密钥将作为我的域的 DNS 记录发布。不幸的是,Office 365 documentation 并不清楚具体的方法。

总结性问题

任何有启发性的总结将不胜感激,谢谢。

来自rfc6376, section 2.6

2.6. Agent or User Identifier (AUID)

A single identifier that refers to the agent or user on behalf of whom the Signing Domain Identifier (SDID) has taken responsibility. The AUID comprises a domain name and an optional <local-part>. The domain name is the same as that used for the SDID or is a subdomain of it. For DKIM processing, the domain name portion of the AUID has only basic domain name semantics; any possible owner-specific semantics are outside the scope of DKIM. It is specified in Section 3.5.

Note that acceptable values for the AUID may be constrained via a flag in the public-key record. (See Section 3.6.1.)

我会接受@jstedfast 的回答(尽管并没有真正理解它)。 为了以防万一其他人正在为此苦苦挣扎,这里是完整的演练:

获得一个public/private密钥对。你可以直接使用 Puttygen 或 openssl,但它更容易使用(哦,我事先只知道)......像 https://port25.com/dkim-wizard/

指定您的域名(example.org 此处)和一个 "selector" - 这可能是您的应用程序名称 ("greatapp")。此选择器将是 DNS 中 public 键的 TXT 记录。 创建额外的 DNS (TXT) 记录;保持 Office365 的完好无损。由于它们会定期轮换密钥,因此您需要一个可以控制的附加记录。 greatapp._domainkey.example.org TXT 格式 "k=rsa\; p=here goes the stuff between -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY-----",例如 "k=rsa\; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhvIwVBomj+dx2CEBbY/ZpSdnQK2Omx6ZNyHsuvC3MMJYNLQ069ajuJo5FP......."

将私钥复制到文件中,或直接在您的代码中使用。 MimeKit 需要一个文件或一个流,所以对于这里的快速和肮脏的例子,我使用的是一个字符串:

var mail = new MimeMessage();
mail.From.Add(new MailboxAddress("Justin Case", "justin@example.org"));
mail.To.Add(new MailboxAddress("Candy Barr", "candy@example.org"));
... subject etc
var privateKey = @"-----BEGIN RSA PRIVATE KEY-----......";
var privateKeyStream = new MemoryStream(Encoding.Default.GetBytes(privateKey));
mail.Sign(new DkimSigner(privateKeyStream, "example.org", "greatapp", DkimSignatureAlgorithm.RsaSha256), new HeaderId[] { HeaderId.From, HeaderId.Subject }, DkimCanonicalizationAlgorithm.Simple, DkimCanonicalizationAlgorithm.Simple);

... Connect client and send.

多亏了 jstedfast,像 MailKit/MimeKit 这样棒的东西存在了,别忘了捐赠。