Issue with Token in Azure web api while trying to access sharepoint - The remote server returned an error: (401) Unauthorized

barry bijoy 26 Reputation points
2021-05-14T14:41:08.497+00:00

I have a azure web api where it access sharepoint and get details from a list using client id client secret.

In last couple of months it suddenly started giving the below error

Microsoft.IdentityModel.SecurityTokenService.RequestFailedException: Token request failed. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2WebRequest.GetResponse()
at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request)

I have already tried the below cases

1)Client id/Secret is still valid, I have already checked that
2) Tried using a new client id and client secret to access SharePoint, by updating web.config using app service editor in azure web api
3)There were no changes done to the code everything was working fine till last month.
4)Tried Accessing the SharePoint site through PowerShell using same client id and secret this works fine without issue.

Any pointers would be helpful

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,890 questions
0 comments No comments
{count} votes

Accepted answer
  1. Paweł Giemza 122 Reputation points
    2021-05-21T12:45:57.903+00:00

    I think the issue might be related to MC240160 Major Update Announcement - see Microsoft 365 message center for more details.

    Check the following links:

    https://learn.microsoft.com/en-us/microsoft-365/compliance/prepare-tls-1.2-in-office-365?view=o365-worldwide
    https://learn.microsoft.com/en-us/sharepoint/troubleshoot/administration/authentication-errors-tls12-support

    Currently I have no service window to check this in our environments, but will give an update when I try.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Grmacjon-MSFT 18,451 Reputation points
    2021-05-18T19:08:24.96+00:00

    Hi @barry bijoy ,

    We apologize for the delay in response. Are you still facing this issue? It looks like this is a known issue as noted on this similar Q&A thread.

    Have you tried running this command based on AmosWu-MSFT's answer?

     Set-SPOTenant -DisableCustomAppAuthentication $false  
    

    Best,
    -Grace


  2. Paweł Giemza 122 Reputation points
    2021-05-21T12:09:06.097+00:00

    We are facing exectly the same issue. What's interesting the same app is working fine when beeing run from a computer running in on-premise network, and fails with 401 unauthorized when is run on the Azure Hosted Virtual machine.


  3. barry bijoy 26 Reputation points
    2021-05-25T10:53:57.487+00:00

    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
    The above code in the startup.Auth.cs in App_start folder fixed the issue thanks a lot @Paweł Giemza

    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.