Microsoft.IdentityModel.SecurityTokenService.RequestFailedException: Token request failed

2021-05-19T06:19:57.057+00:00

I have the following CSOM code:-

using (ClientContext context = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(RisksiteUrl, clientId, clientSecret, AzureEnvironment.Production))

it was working well for around 1 year, but starting from yesterday the above code will raise this exception:-

Unhandled Exception: Microsoft.IdentityModel.SecurityTokenService.RequestFailedException: Token request failed. ---> System.Net.WebException: The remote server
returned an error: (401) Unauthorized.

Any advice?
Thanks

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-05-20T08:22:49.693+00:00

    Hi @Quanta Dialysis Technologies Limited ,

    Maybe the client secret expired, so you got this error. By default, client secrets for SharePoint Add-ins that are registered by using the AppRegNew.aspx page expire after one year.

    You could renew the client secret: https://medium.com/@cecildt/renewing-sharepoint-online-provider-add-ins-client-secret-ba2828a49e7

    Or register a new app to make it work.

    Update:

    This could be related with the TLS 1.2 enforcement that's being rolled out. Read this article for tetails: https://techcommunity.microsoft.com/t5/microsoft-sharepoint-blog/provider-hosted-app-pha-application-errors-tls-errors-and-401/ba-p/2273611


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.


  2. Leonardo da Silva Brito 1 Reputation point
    2022-09-23T20:31:28.98+00:00

    It is necessary to look at the exception message, because in my case I had a solution that had two projects and after installing the pnp framework the solution was with newtonsoft.json v12 and in the other project it was with newtonsoft.json v10, I discovered this by the message from exception - "Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of the important dependencies. The assembly manifest definition does not match the reference to the assembly. ( Exception from HRESULT: 0x80131040)":"Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed", after discovering this I updated them both to have v12 and it worked.

    0 comments No comments

  3. John Guilbert (Housing) 1 Reputation point
    2023-08-14T15:01:35.7966667+00:00

    I'm getting this issue in Debug Mode in VS 2022 and I don't understand it. Works fine with same code on Azure App Service. Anyone out there getting same issue? Only started recently and I did upgrade to Windows 11 a few months ago and haven't run code for a while. Powershell 7 I can connect to SharePoint App Reg. Not expired either. Intermittent access. It must be related to Project or Visual Studio 2022

    0 comments No comments

Your answer

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