PnP-Sites-Core icon indicating copy to clipboard operation
PnP-Sites-Core copied to clipboard

AuthenticationManager().GetAppOnlyAuthenticatedContext failing in dotnet Core

Open donshults opened this issue 5 years ago • 15 comments

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();
    }

donshults avatar Jan 15 '19 20:01 donshults

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

kmcknight1973 avatar Feb 04 '19 10:02 kmcknight1973

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

ShriramDeshkar avatar Jun 30 '19 06:06 ShriramDeshkar

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?

Ofer-Gal avatar Oct 23 '19 23:10 Ofer-Gal

I am facing the same issue. Any updates?

gautamsyadav avatar Nov 25 '19 18:11 gautamsyadav

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)

christophwille avatar Dec 04 '19 14:12 christophwille

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

rbleattler avatar Apr 06 '20 14:04 rbleattler

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

westerdaled avatar May 06 '20 13:05 westerdaled

Any new regarding this issue?

adrichema avatar May 26 '20 13:05 adrichema

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

ypcode avatar Jun 02 '20 14:06 ypcode

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. :)

luizs81 avatar Sep 01 '20 06:09 luizs81

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?

piteerus avatar Sep 16 '20 06:09 piteerus

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

garrjo avatar Sep 26 '20 01:09 garrjo

Do you think can we rewrite TokenHelper.cs?

antonio-gabriele avatar Sep 28 '20 12:09 antonio-gabriele

Same issue :(

svermaak avatar Sep 30 '20 02:09 svermaak

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()

dnyaneshwar-surywanshi avatar Oct 09 '20 05:10 dnyaneshwar-surywanshi