hi together,
we are facing the same problem.
We are trying to connect via OAuth2 and Pop3 without user interaction. (ClientCredentials)
We did register an App in our Microsoft Tenant with all delegated permissions and APP Permissions correlating to Mail access. We use this registered app
with a client secret to get the token from "
https://login.microsoftonline.com/MSTenantID/oauth2/v2.0/token", where MSTenantID is our Microsoft Tenant.
var scope = "
https://outlook.office.com/.default";
We are requesting the token correctly. Connection via EWS is possible. A connection via Pop3 always fails with the following error:
Rebex.Net.Pop3Exception: "Authentication failure: unknown user name or bad password."
Example Code:
static void Main(string[] args)
{
Pop3 pop3 = new Pop3();
try
{
pop3.Connect(server,SslMode.Implicit);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
var tok = HelperClass.GetElibilityToken(tenant_id, client_ID, client_Secret, scope);
//string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", userMail, '\x1', tok.AccessToken);
string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", userMail, Convert.ToChar(0x01), tok.AccessToken);
string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));
try
{
pop3.Login(token, Pop3Authentication.OAuth20);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
public static Token GetElibilityToken(string ptenantID,string pclient_id, string pclient_secret, string pScope)
{
// Using Microsoft.Identity.Client 4.22.0
var cca = ConfidentialClientApplicationBuilder
.Create(pclient_id)
.WithClientSecret(pclient_secret)
.WithTenantId(ptenantID)
.Build();
// The permission scope required for EWS access
var ewsScopes = new string[] { pScope };
//Make the token request
var authResult = cca.AcquireTokenForClient(ewsScopes).ExecuteAsync().Result;
Token tok = new Token();
tok.AccessToken = authResult.AccessToken;
tok.TokenType = authResult.TokenType;
return tok;
}
Is there any solution without EWS? Is there something missing in the implementation?
Thanks for the help.