When Microsoft revoked the Basic Authentication for IMAP protocol (past year), I implemented a way to get the token to use in the connection. I'm using Aspose.Email and I followed that article: https://docs.aspose.com/email/net/access-mail-services-using-oauth/#implementation-of-custom-itokenprovider-for-office-365
But that way doesn't support MFA (Multi-Factor Authentication) accounts, and I'm trying to find the "better" way (for my app) to implement the full flow, and think at the same time, with the SMTP protocol, that I use to send mails in batch processes.
The question summarized is: Is there any way to connect to Azure IMAP (or SMTP) with OAuth and MFA without user interaction?
I've crawled in internet, and I've found that solution, but I don't like it (must assign permissions to all mailboxes, manually), anyway I must try it, when I can test with the Azure admin: https://learn.microsoft.com/en-us/answers/questions/1112032/outlookoffice365-imap-how-to-get-access-token(api)#answers
If the solution requires user interaction (I understand that it would be the way), how could I refresh the token when it will expire? Some kind of cookie?
I've separated applications for the front (ASPNET MVC) and back (.NET WCF Services), and the connection to IMAP must be established in back.
I've published a question in Aspose.Email forum too: https://forum.aspose.com/t/aspose-mail-connect-to-azure-imap-oauth-via-ropc-in-a-mfa-account/276114
Finally I've decided to implement the validation flow through Azure SSO web, because I haven't found any easy way to implement it without user interaction. So I've implemented two mockup apps (ASPNET MVC Web App, and Console App) to simulate the same environment of my app:
ASPNET MVC Web App:
Console App:
Web mockup code (Controller):
Console mockup code:
And the FileBasedTokenCache shared class: