Quantcast
Channel: Authentication failed exception with MailKit OAuth2.0 - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Authentication failed exception with MailKit OAuth2.0

$
0
0

I'm using the following code to get an access token and connect to the mail folder:

var confidentialClientApplicationBuilder = ConfidentialClientApplicationBuilder.Create(clientId).WithClientSecret(clientSecret).WithTenantId(tenantId).Build();var scopes = new string[] { ".default" };var authToken = await confidentialClientApplicationBuilder.AcquireTokenForClient(scopes).ExecuteAsync();var oauth2 = new SaslMechanismOAuth2(username, authToken.AccessToken);using (ImapClient client = new ImapClient()){    await client.ConnectAsync("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);    await client.AuthenticateAsync(oauth2);    //TODO    await client.DisconnectAsync(true);}

Everything seems to work correctly here, the ImapClient is connected and I can see oauth2.Credentials.Password is populated with the access token. However, when I run it the AuthenticateAsync method throws the error:

MailKit.Security.AuthenticationException: 'Authentication failed.'

I have noticed that the authToken.Account is null and that's why I'm passing the account name in by the string username. Also it seems I have to use the .default scope as anything else causes an error on AcquireTokenForClient as per this question.

Any ideas what I'm doing wrong here?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images