-
Notifications
You must be signed in to change notification settings - Fork 661
Add Windows Auth Request Header for CurrentCredentials #2571
Conversation
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
Tested against SharePoint 2019. Works well. Nice one @PowershellNinja! It will be merged through PR #2572 |
@KoenZomers Just found out that the fix in this PR does not work for neither Get-PnPProvisioningTemplate nor Apply-PnPProvisioningTemplate. 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). using (var handlerContext = web.Context.Clone(web.Url))
{
template = handler.ExtractObjects(handlerContext.Web, template, creationInfo);
} |
I am testing gainst SharePoint 2013 and it already fails with Get-PnPProvisioningTemplate without any further parameters |
@PowershellNinja Could you have Fiddler running while doing the |
Actually it seems I also have a fix for this issue. Going to open a PR in the PnP-Sites-Core Repo. // 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. |
Nice find! |
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
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.