Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Add Windows Auth Request Header for CurrentCredentials #2571

Closed
wants to merge 1 commit into from

Conversation

PowershellNinja
Copy link
Contributor

Add a delegate to inject the "X-FORMS_BASED_AUTH_ACCEPTED" request header into the context to allow for successful windows authentication even when more than one authentication provider is active on the target webapplication

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #2570

What is in this Pull Request ?

When using -CurrentCredentials with Connect-PnPOnline, the Request Header "X-FORMS_BASED_AUTH_ACCEPTED" is injected into every Web Request to allow for Windows Authentication when more than one Authentication Provider is active on the OnPremises Web Application.

Add a delegate to inject the "X-FORMS_BASED_AUTH_ACCEPTED" request header into the context to allow for successful windows authentication even when more than one authentication provider is active on the target webapplication
@KoenZomers
Copy link
Collaborator

Tested against SharePoint 2019. Works well. Nice one @PowershellNinja! It will be merged through PR #2572

@PowershellNinja
Copy link
Contributor Author

PowershellNinja commented Mar 13, 2020

@KoenZomers Just found out that the fix in this PR does not work for neither Get-PnPProvisioningTemplate nor Apply-PnPProvisioningTemplate.
Going to investigate, but do you have any idea what these Cmdlets do different?

Edit: Seems the be an issue in PnP.Sites.Core:

powershell.exe Information: 0 : 2020-03-13 15:54:43.1665	[OfficeDevPnP.Core]	[0]	[Information]	File _template.xml retrieved from folder 	0ms	
powershell.exe Information: 0 : 2020-03-13 15:54:43.1674	[OfficeDevPnP.Core]	[0]	[Information]	File _template.xml retrieved from folder 	0ms	
powershell.exe Information: 0 : 2020-03-13 15:54:43.1724	[Provisioning]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.1724	[Provisioning]	[10]	[Information]	ProgressDelegate registered	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.1724	[Provisioning]	[10]	[Information]	MessagesDelegate registered	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2345	[Extensibility Providers]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2345	[Extensibility Providers]	[10]	[Debug]	Code execution scope ended	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2345	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2345	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope ended	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2495	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2495	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope ended	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.2505	[Fields]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Error: 0 : 2020-03-13 15:54:43.3185	[OfficeDevPnP.Core]	[0]	[Error]	ExecuteQuery threw following exception: System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay, String userAgent).	0ms	
powershell.exe Information: 0 : 2020-03-13 15:54:43.3185	[Fields]	[10]	[Debug]	Code execution scope ended	68ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.3185	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope started	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.3185	[ProvisioningTemplate WebHook Call]	[10]	[Debug]	Code execution scope ended	0ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a
powershell.exe Information: 0 : 2020-03-13 15:54:43.3195	[Provisioning]	[10]	[Debug]	Code execution scope ended	146ms	9d381ffd-bd03-4a27-9b41-e8fa5499db8a

Second Edit: I have a guess what might be the problem (Can't properly debug it right now though).
In this line in PnP-Sites-Core, the Web.Context is cloned, my guess is that any existing EventHanders / Delegates are not included in this copy:
https://github.com/SharePoint/PnP-Sites-Core/blob/c09e0f0590aada578d6b958b532b396e044ffcd4/Core/OfficeDevPnP.Core/Framework/Provisioning/ObjectHandlers/SiteToTemplateConversion.cs#L150

using (var handlerContext = web.Context.Clone(web.Url))
{
     template = handler.ExtractObjects(handlerContext.Web, template, creationInfo);
}

@KoenZomers
Copy link
Collaborator

Just running Get-PnPProvisioningTemplate already fails for you? It works for me and nicely includes the header in all its requests:

image

Are you using any specific arguments with Get-PnPProvisioningTemplate? You're still using SharePoint 2013? I'm testing against SharePoint 2019 at the moment.

@PowershellNinja
Copy link
Contributor Author

I am testing gainst SharePoint 2013 and it already fails with Get-PnPProvisioningTemplate without any further parameters

@KoenZomers
Copy link
Collaborator

@PowershellNinja Could you have Fiddler running while doing the Get-PnPProvisioningTemplate so you can validate if the X-FORMS_BASED_AUTH_ACCEPTED header gets included in the request?

@PowershellNinja
Copy link
Contributor Author

PowershellNinja commented May 11, 2020

Actually it seems I also have a fix for this issue. Going to open a PR in the PnP-Sites-Core Repo.
If I add the following code in file
https://github.com/pnp/PnP-Sites-Core/blob/c09e0f0590aada578d6b958b532b396e044ffcd4/Core/OfficeDevPnP.Core/Extensions/ClientContextExtensions.cs#L317
below line 317, Get-PnPProvisioningTemplate and Apply-PnPProvisioningTemplate start working like a charm even when using Windows Authentication:

// In case of existing Event Handlers
                clonedClientContext.ExecutingWebRequest += (sender, webRequestEventArgs) =>
                {
                    // Call the ExecutingWebRequest delegate method from the original ClientContext object, but pass along the webRequestEventArgs of 
                    // the new delegate method
                    MethodInfo methodInfo = clientContext.GetType().GetMethod("OnExecutingWebRequest", BindingFlags.Instance | BindingFlags.NonPublic);
                    object[] parametersArray = new object[] { webRequestEventArgs };
                    methodInfo.Invoke(clientContext, parametersArray);
                };

Essentially, this piece of code reattaches all Eventhandlers from the original context to the cloned context.

@KoenZomers
Copy link
Collaborator

Nice find!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants