Teams bot SDK returns intermittent 401 Unauthorized errors when sending messages

Arjun Mehra 80 Reputation points
2025-10-10T10:21:16.5766667+00:00

The bot is hosted in Azure App Service and connected through Azure Bot Channels Registration. Most of the time, it works fine but occasionally, when sending proactive or reply messages to users, the bot fails with a 401 Unauthorized error from the Microsoft API.

The strange part is that the same code works again a few minutes later without any change. I’ve verified the Microsoft App ID and password, checked token caching, and confirmed that the bot registration is valid.

Here’s the code section where the error occur

await turnContext.SendActivityAsync(
    MessageFactory.Text("Hello! This is a test message."),
    cancellationToken);

The authentication token appears to expire randomly, and I’m not sure if the issue is related to Teams channel auth caching or the App Service identity refresh timing.

Microsoft Teams | Development
0 comments No comments
{count} votes

Answer accepted by question author
  1. Flora-T 5,875 Reputation points Microsoft External Staff Moderator
    2025-10-10T12:42:03.7566667+00:00

    Hi Arjun Mehra

    Thank you for reaching out to Microsoft Q&A Forum and sharing details about your Teams bot setup.  

    I've looked into similar reports on intermittent 401 Unauthorized errors, and based on my research, there still aren't any definitive suggestions or fixes from comparable cases involving Teams bots with this exact intermittent auth issue. 

    The default lifetime of an access token in the Bot Framework is variable, it's assigned a random value between 60-90 minutes (averaging around 75 minutes) when issued. This may contribute to the randomness you're seeing if tokens aren't refreshing as expected. Details are outlined in this article: https://learn.microsoft.com/en-us/entra/identity-platform/access-tokens#token-lifetime

    User's image

    Based on your description, it seems the issue may not lie with your code, as it performs reliably most of the time, with the errors occurring sporadically and resolving themselves without any modifications. In the meantime, you may consider implementing retry logic with exponential backoff as a workaround to help mitigate these transient 401 errors. You may refer to these Microsoft resources for further information: 

    Additionally, keep an eye on the health of your Azure Bot Service and the Teams channel. You can monitor for any ongoing service issues via the Azure portal here: Azure Health - Service Issues

    Please let me know how you get on, as your feedback is valuable to the community. I'm looking forward to seeing your response soon.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.