如何使用 MailKit 中的 OAuth 2.0 连接到 Exchange Online? [英] How do I connect to Exchange Online using OAuth 2.0 in MailKit?

查看:64
本文介绍了如何使用 MailKit 中的 OAuth 2.0 连接到 Exchange Online?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,它使用 MailKit 和基本身份验证通过 Exchange Online (Office365) 向用户发送电子邮件.我们公司是 MS 合作伙伴,因此有义务在 2020 年 2 月底之前为我们的服务关闭基本身份验证.

所以,我想使用 OAuth 2.0 连接到 Exchange Online,类似于此 示例.事实上,根据这个 answer 可能有可用的解决方案,但我找不到任何相关信息.p>

现在我正在使用 MS Identity Platform v2.0,但我不知道该怎么做.

如有任何帮助,将不胜感激.

更新 1

我不想代表已登录的用户发送邮件,而是有一个 Office365 用户帐户可用于向其他人发送邮件(通知等).

更新 2

我设法使用 Microsoft Graph SDK 和 用户名/密码提供者.

我们的用户帐户需要多因素身份验证,因此在使用用户密码时出现错误,因为我无法满足第二个因素.当我使用应用程序密码时,由于密码错误,身份验证失败.

更新 3

我现在切换到邮件中继.但如果我能找到答案,我会更新这个问题.

解决方案

使用 Microsoft.Identity.Client 您可以生成一个令牌并通过然后使用它进行身份验证.

我发现下面的 IMAP、POP3 和 SMTP 非常适合我的项目以获得有效的解决方案.尽管该示例显示了交互式方法,但我正在尝试将客户端凭据流与应用程序机密一起使用.

MailKit - 将 OAuth2 与 Exchange 结合使用(IMAP、POP3 或 SMTP)

Microsoft - 使用 OAuth 验证 IMAP、POP 或 SMTP 连接

I have a web application that sends e-mails to users via Exchange Online (Office365) using MailKit and Basic Authentication. Our company is MS partner and therefor is obligated to turn off Basic Authentication for our services by the end of february 2020.

So, I want to use OAuth 2.0 to connect to Exchange Online, similar to this example. In fact, there might be a solution available according to this answer but I'm unable to find anything about it.

Right now I'm playing around with MS Identity Platform v2.0 but I'm unable to figure out how to do it.

Any help would be appriciated.

UPDATE 1

I do not want to send mails on behalf of signed-in users but instead there is a single Office365 user account that shall be used to send mails (notifications and so on) to others.

UPDATE 2

I managed to get a little closer to what I want to do using Microsoft Graph SDK and the Username/Password Provider.

Our user account requires multifactor-authentication and therefor I get an error when using the user's password since I cannot satisfy the second factor. When I'm using an app-password authentication fails because of incorrect password.

UPDATE 3

I switched to mail relaying for now. But I will update this question if I'll ever find an answer to it.

解决方案

Using the Microsoft.Identity.Client you can generate a token and pass though then authentication using that.

I spotted the below for IMAP, POP3 and SMTP so adapted for my project to get a working solution. Although the example show the interactive method, where as I am was trying to use the the client credentials flow with an app secret.

MailKit - Using OAuth2 With Exchange (IMAP, POP3 or SMTP)

Microsoft - Authenticate an IMAP, POP or SMTP connection using OAuth

这篇关于如何使用 MailKit 中的 OAuth 2.0 连接到 Exchange Online?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆