jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

await client.AuthenticateAsync (oauth2); !Authentication failed!

NeroLiang19 opened this issue · comments

commented

Describe the bug

await client.AuthenticateAsync (oauth2); //Failed!

Platform (please complete the following information):

  • OS: [e.g. Windows11.]
  • .NET Runtime: [NET6]
  • .NET Framework: [e.g. .Net Core,.]
  • MailKit Version: 4.1.0

Exception
Authentication failed!

var clientId = "xxxxxxxxxb-a0ad-xxxx-b686-xxxxxxxxxx9";
var tenantId = "xxxxxxxxxx-9414-xxxxxx-a489-xxxxxxxxxde";
var redirectUri = "http://localhost";
var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxSt";

		string userName = "xxxxxx@neewer.com";   

		var app = ConfidentialClientApplicationBuilder
			.Create(clientId)
			.WithTenantId(tenantId)
			.WithClientSecret(clientSecret)
			.Build();

		string[] scopes = { 
			"https://outlook.office365.com/.default"
		};
		
		var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
		string accessToken = result.AccessToken;

		using (var client = new ImapClient())
		{
			try {
				await client.ConnectAsync ("outlook.office365.com", 993,SecureSocketOptions.SslOnConnect);
				await client.AuthenticateAsync (new SaslMechanismOAuth2 (userName, accessToken));//Failed!!!
				await client.DisconnectAsync (true);
				var inbox = client.Inbox;
			} catch (Exception e) {
				Console.WriteLine (e);
				throw;
			}
		} 

Note: if the protocol log contains sensitive information, feel free to email it to me at
jestedfa@microsoft.com instead of including it in the GitHub issue.

Additional context
Add any other context about the problem here.

commented

image

This is a duplicate of issue #1126