"AADSTS50196: The server terminated an operation because it encountered a client request loop"

Using OAuth2, we receive the error in the topic after processing 40 or so email messages. Have you seen this error and have any idea of how to avoid it?

I’m working with a Windows service that runs unattended with username and password access, but it seems like the service runs too quickly for Azure to handle and we hit some sort of flood control. While I can making things work with a hardcoded 5 minute wait when the exception is encountered, this reduces the performance of the service to the point of being unusable.

Here’s my code for retrieving a token:

internal static class OAuthTokenProvider
{
    public static OAuthNetworkCredential GetAzureToken(ref IPublicClientApplication app, string clientId, string tenantId, string redirectUri, string user, string pass, string[] scopes)
    {
        AuthenticationResult result = null;

        if (app == null)
        {
            app = PublicClientApplicationBuilder
                .Create(clientId)
                .WithRedirectUri(redirectUri)
                .WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
                .Build();
        }

        var accounts = app.GetAccountsAsync().Result;

        try
        {
            if (accounts.Any())
            {
                result = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                    .WithForceRefresh(true)
                    .ExecuteAsync().Result;
            }
            else
            {
                result = app.AcquireTokenByUsernamePassword(scopes, user, new NetworkCredential("", pass).SecurePassword).ExecuteAsync().Result;
            }
        }
        catch (Exception ex)
        {
            if (ex.InnerException != null && ex.InnerException.Message.Contains("AADSTS50196"))
            {
                Thread.Sleep(300000); // I want to avoid this
                result = app.AcquireTokenByUsernamePassword(scopes, user, new NetworkCredential("", pass).SecurePassword).ExecuteAsync().Result;
            }
            else
            {
                throw;
            }
        }

        return new OAuthNetworkCredential(result.AccessToken);
    }
}

Thanks!

@JDNaviant

There is no such limitation in API. Are you using license for loading emails? Please share the details of the issue incurring on your end so that we may investigate it further.

There is no such limitation in API.

Yes, I didn’t expect there to be. This error is from Azure, and Aspose is simply forwarding it. What I’d like to know is if anyone has ever seen this before, because I’m also working with Microsoft on this issue and they understandably were nervous about a third party library that’s effectively a black box.

Are you using license for loading emails?

I don’t understand the question. I’m using a valid license for Aspose.Email, if that’s what you mean.

Please share the details of the issue incurring on your end so that we may investigate it further.

The issue is quite literally just making a lot of requests to the mail server in quick succession. It’s not a problem with basic authentication, but when OAuth is thrown into the mix, we start seeing the error.

@JDNaviant

Based on the information provided it doesn’t seems to be an API issue more on Microsoft Azure end when you make quick requests for authentications. However, I have created a ticket with ID EMAILNET-40074 in our issue tracking system to further investigate this on our end too.
I have also found similar issue discussion over the internet and suggest you to please read the information in following link if that helps.