How do I connect to Exchange Online using OAuth 2.0 in MailKit?

安稳与你 提交于 2020-03-21 10:11:21

问题


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.


回答1:


My choice would be to look into Microsoft Graph API . It is a single endpoint for all Microsoft services including Email. Email specific endpoints document is here

Microsoft provides SDK in different languages to develop client applications using Graph API.

At a high level you would need to do the following.

i) Register an application in Azure Active Directory. See here

ii) Use the Oauth2 'authorization code grant' flow to get a refresh token . See here

iii) Exchange the refresh token for an access token and use the access token to call Microsoft Graph API.

iv) You also need to store the refresh token , if you have use cases where you application needs to perform actions even if the user is offline. In this case make sure you include scope 'offline' in step ii)




回答2:


I would suggest looking into DotNetOpenAuth or a similar library and reading their samples. You'll probably need to know the Windows Live URLs to use for this if the DotNetOpenAuth library doesn't have them built-in.

Samples can be found here: https://github.com/DotNetOpenAuth/DotNetOpenAuth.Samples



来源:https://stackoverflow.com/questions/59881654/how-do-i-connect-to-exchange-online-using-oauth-2-0-in-mailkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!