IMAP Authentication Failed with access token
See original GitHub issueHello, I am sorry for opening this issue but after I searched a lot I still couldn’t understand if what I am trying to do is possible or if I am doing it wrong.
I got an access token from the OAuth 2.0 authorization code flow (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow). I know that until this step everything is fine because I can refresh the token with success.
I am trying to use that access token, in ImapClient.Authenticate(), but I get the “Authentication failed” message, as if I am using wrong credentials. I thought it would be possible to just have the token and use it in the Authenticate() method. Am I doing anything wrong or is it only possible to use the access token retrieved with the PublicClientApplication like in the example in this link (https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md) ?
Example code that fails for me: (MailKit Version=2.10.0.0)
const string username = "example@hotmail.com";
string accesstoken = "accesstoken"; (retrieved from the auth code flow successfully)
using (var imapClient = new ImapClient(new ProtocolLogger("imap.log")))
{
imapClient.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
try
{
imapClient.Authenticate(new SaslMechanismOAuth2(username, accesstoken));
}
catch (Exception ex)
{
// irrelevant in my case since I am running a testing app just for this
}
}
StackTrace:
at MailKit.Net.Imap.ImapClient.<AuthenticateAsync>d__81.MoveNext() in D:\src\MailKit\MailKit\Net\Imap\ImapClient.cs:line 850
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MailKit.Net.Imap.ImapClient.Authenticate(SaslMechanism mechanism, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\Imap\ImapClient.cs:line 913
Protocol log:
Connected to imaps://outlook.office365.com:993/
S: * OK The Microsoft Exchange IMAP4 service is ready. [UABSADMAUAAxADkAMgBDAEEAMAAwADIANwAuAEUAVQBSAFAAMQA5ADIALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
S: A00000000 OK CAPABILITY completed.
C: A00000001 AUTHENTICATE XOAUTH2 ${token}
S: A00000001 NO AUTHENTICATE failed.
Once again, I am sorry if there is already an answer for this and I couldn’t find it. Hope you can help. Thank you.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Related StackOverflow Question
My guess is that if it’s not working for you, then you probably aren’t getting the correct access_token.
I’m not sure what you could be doing wrong in getting the access_token you are getting, but I can confirm that the code I posted in the ExchangeOAuth2.md file works.
I would definitely recommend starting with the code I wrote in that guide.
Awesome! Glad it was something as simple as the scope!