Unable to use “OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext” inside ASP.NET core (.NET 5)

john john 986 Reputation points
2021-03-30T20:32:11.023+00:00

I have the following CSOM code inside my ASP.NET CORE (.NET 5.0):-

var siteUrl = config.GetSection("SP").GetSection("SiteURL").Value;             
string clientId = "****";  
string clientSecret = "***";  
  
     using (ClientContext context = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))  
  
        {  

but i am getting this error:-

System.TypeInitializationException  
  HResult=0x80131534  
  Message=The type initializer for 'OfficeDevPnP.Core.Utilities.TokenHelper' threw an exception.  
  Source=OfficeDevPnP.Core  
  StackTrace:  
   at OfficeDevPnP.Core.Utilities.TokenHelper.set_Realm(String value)  
   at OfficeDevPnP.Core.AuthenticationManager.EnsureToken(String siteUrl, String realm, String appId, String appSecret, String acsHostUrl, String globalEndPointPrefix)  
   at OfficeDevPnP.Core.AuthenticationManager.GetAppOnlyAuthenticatedContext(String siteUrl, String realm, String appId, String appSecret, String acsHostUrl, String globalEndPointPrefix)  
   at OfficeDevPnP.Core.AuthenticationManager.GetAppOnlyAuthenticatedContext(String siteUrl, String appId, String appSecret)  
   at ServiceDeskSharePointIntegration.Program.Main(String[] args) in C:\Users\****\source\repos\ServiceDeskSharePointIntegration\ServiceDeskSharePointIntegration\Program.cs:line 47  
  
Inner Exception 1:  
TypeLoadException: Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.  

when i hover over the related package i got this error:-

82955-left.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,175 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amos Wu-MSFT 4,051 Reputation points
    2021-03-31T07:16:42.783+00:00

    SharePointPnPCoreOnline is no longer maintained, please use PnP Framework (https://github.com/pnp/pnpframework) going forward.
    And test code for your reference:

    ClientContext ctx = new AuthenticationManager().GetACSAppOnlyContext("https://contoso.sharepoint.com/sites/dev", "app id", "app secret", AzureEnvironment.Production);  
    

    If the response 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.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

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.