Pnp-sites-core: AuthenticationManager().GetAppOnlyAuthenticatedContext failing in dotnet Core

Created on 15 Jan 2019  路  15Comments  路  Source: pnp/PnP-Sites-Core

Did a quick test of a the .GetAppOnlyAuthenticatedContext. One using Framework 4.6.1, the other using Core 2.1. The Framework version works fine and returns the title of each list.
Here's the error when running in Core:
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'System.Runtime.Remoting.ObjectHandle System.Activator.CreateInstance(System.String, System.String)'.
Source=OfficeDevPnP.Core
StackTrace:
at OfficeDevPnP.Core.Diagnostics.Log.InitializeLogger()
at OfficeDevPnP.Core.Diagnostics.Log.Debug(String source, String message, Object[] args)
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 appId, String appSecret)
at TestPNPTool.Program.Main(String[] args) in C:\Users\donald.shults\Source\Repos\SPOListUpdater\TestPNPTool\Program.cs:line 17

Here's the test code using a simple console app:

    static void Main(string[] args)
    {
        string siteUrl = "https://<tenant url>/sites/spdev";
        string clientId = "<clientid>";
        string clientSecret = "<appsecret>";

        using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
        {
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
            clientContext.Load(collList);
            clientContext.ExecuteQuery();
            foreach (List list in collList)
            {
                Console.WriteLine("Name: " + list.Title);
            }
        }

        Console.ReadLine();
    }

Most helpful comment

Yes, I'll be very interested for support on core 2.1 as I've encountered the same issue. Any work around on this?

All 15 comments

Yes, I'll be very interested for support on core 2.1 as I've encountered the same issue. Any work around on this?

I m facing the same error while using AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant() method using .NetCore2.1
Any suggestions on how to resolve this ?

When used with framework, I got the warning:
Warning NU1701 Package 'Microsoft.SharePointOnline.CSOM 16.1.19404.12000' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v2.2'

So now I have it running in netcoreapp2.2 but I get the same error on OfficeDevPnP.Core.GetAppOnlyAuthenticatedContext
Is there another way to get App Only Authenticated Context without OfficeDevPnP.Core?

I am facing the same issue. Any updates?

Also interested in .NET Core support (my target: 3.1). For me

var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(url, clientId, clientSecret);

is failing with

            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 CoreTestSharepoint.Program.GetSiteCollectionWithAppOnly(String clientId, String clientSecret, String url) in d:\TfsWorkspace\CoreTestSharepoint\CoreTestSharepoint\Program.cs:line 27
               at CoreTestSharepoint.Program.Main(String[] args) in d:\TfsWorkspace\CoreTestSharepoint\CoreTestSharepoint\Program.cs:line 22

            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'.

(I added System.Configuration.ConfigurationManager manually because it failed there first - but System.Web is definitely full stop for .NET Core)

Has this issue seen any movement? I'm hitting this as well trying to Connect using PowerShell 7.

@rbleattler , glad you posted, as I have been scratching my head for 30 mins on this issue untll I realised I hadn't change my Shell to Windows PowerShell in VSCode from PowerShell Core 7 馃槪 . SharePointPnPPowerShellOnline Commandlets are not support in PowerShell 7.
Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url $in
I am now connected at last....

Any new regarding this issue?

Hi guys,

as far as I know, PnP Sites Core is currently not supporting .NET Core since CSOM is not supporting it either for now

But that should come quite soon ! Anyway, it will come with some limitations (e.g. SharePointOnlineCredentials not supported, ...)
You will have some info about it in the recent SIG call here https://youtu.be/gtusCyOYePs?t=890

It seems that now Microsoft.SharePointOnline.CSOM supports .NET Core.
https://developer.microsoft.com/en-us/microsoft-365/blogs/net-standard-version-of-sharepoint-online-csom-apis/

I know it's not easy but I hope the PnP team can catch up. :)

Guys,
For .NET Standard 2.1 I'm receiving the following error:
OfficeDevPnP.Core: Could not load type 'System.Web.UI.HtmlTextWriter' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Could you please take a look and comply with netstandard2.1?

Still an issue on .NET Standard 3.1 - > Exception Type Initializer: OfficeDevPnP.Core.Utilities.TokenHelper.

Do you think can we rewrite TokenHelper.cs?

Same issue :(

I am using .net core 3.1 and getting same issue :

System.TypeInitializationException: The type initializer for 'OfficeDevPnP.Core.Utilities.TokenHelper' threw an exception.
---> System.TypeLoadException: Could not load type 'System.Web.Configuration.WebConfigurationManager' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
at OfficeDevPnP.Core.Utilities.TokenHelper..cctor()

Was this page helpful?
0 / 5 - 0 ratings