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

Fix: #2786 - Pass cookiecontainer to HttpClientHandler to support provisioning client side pages on tenant with Federated authentication login #2787

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions Core/OfficeDevPnP.Core/Pages/ClientSidePage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AngleSharp.Parser.Html;
using AngleSharp.Parser.Html;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand Down Expand Up @@ -78,6 +78,7 @@ public class ClientSidePage
private string sitePagesServerRelativeUrl;
private bool securityInitialized = false;
private string accessToken;
private CookieContainer cookies;
private readonly List<CanvasSection> sections = new List<CanvasSection>(1);
private readonly List<CanvasControl> controls = new List<CanvasControl>(5);
private readonly List<CanvasControl> headerControls = new List<CanvasControl>();
Expand Down Expand Up @@ -1578,7 +1579,7 @@ public System.Collections.Generic.IEnumerable<ClientSideComponent> AvailableClie
}

// Request information about the available client side components from SharePoint
string availableClientSideComponentsJson = await GetClientSideWebPartsAsync(this.accessToken, this.Context);
string availableClientSideComponentsJson = await GetClientSideWebPartsAsync(this.accessToken, this.cookies, this.Context);

if (String.IsNullOrEmpty(availableClientSideComponentsJson))
{
Expand Down Expand Up @@ -1618,7 +1619,7 @@ public TranslationStatusCollection GenerateTranslations(TranslationStatusCreatio
this.InitializeSecurity();
}

Task<String> result = Task.Run(() => GenerateTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId, translationStatusCreationRequest).GetAwaiter().GetResult());
Task<String> result = Task.Run(() => GenerateTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId, translationStatusCreationRequest).GetAwaiter().GetResult());

if (!string.IsNullOrEmpty(result.Result))
{
Expand All @@ -1644,7 +1645,7 @@ public TranslationStatusCollection Translations()
this.InitializeSecurity();
}

Task<string> result = Task.Run(() => GetTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId).GetAwaiter().GetResult());
Task<string> result = Task.Run(() => GetTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId).GetAwaiter().GetResult());

if (!string.IsNullOrEmpty(result.Result))
{
Expand Down Expand Up @@ -1672,7 +1673,7 @@ public async Task<TranslationStatusCollection> TranslationsAsync()
await this.InitializeSecurityAsync();
}

string result = await GetTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId);
string result = await GetTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId);

if (!string.IsNullOrEmpty(result))
{
Expand All @@ -1697,7 +1698,7 @@ public TranslationStatusCollection GenerateTranslations()
this.InitializeSecurity();
}

Task<string> result = Task.Run(() => GenerateTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId, null).GetAwaiter().GetResult());
Task<string> result = Task.Run(() => GenerateTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId, null).GetAwaiter().GetResult());

if (!string.IsNullOrEmpty(result.Result))
{
Expand Down Expand Up @@ -1725,7 +1726,7 @@ public async Task<TranslationStatusCollection> GenerateTranslationsAsync(Transla
await this.InitializeSecurityAsync();
}

string result = await GenerateTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId, translationStatusCreationRequest);
string result = await GenerateTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId, translationStatusCreationRequest);

if (!string.IsNullOrEmpty(result))
{
Expand All @@ -1752,7 +1753,7 @@ public async Task<TranslationStatusCollection> GenerateTranslationsAsync()
await this.InitializeSecurityAsync();
}

string result = await GenerateTranslationsImplementationAsync(this.accessToken, this.Context, this.PageId, null);
string result = await GenerateTranslationsImplementationAsync(this.accessToken, this.cookies, this.Context, this.PageId, null);

if (!string.IsNullOrEmpty(result))
{
Expand Down Expand Up @@ -2370,7 +2371,7 @@ private void ApplySectionAndColumn(CanvasControl control, ClientSideCanvasContro
}
}

private async Task<string> GetTranslationsImplementationAsync(string accessToken, ClientContext context, int? pageID)
private async Task<string> GetTranslationsImplementationAsync(string accessToken, CookieContainer cookies, ClientContext context, int? pageID)
{
await new SynchronizationContextRemover();

Expand All @@ -2397,6 +2398,10 @@ private async Task<string> GetTranslationsImplementationAsync(string accessToken
{
handler.Credentials = networkCredential;
}
else
{
handler.CookieContainer = cookies;
}
}

using (var httpClient = new PnPHttpProvider(handler))
Expand Down Expand Up @@ -2435,7 +2440,7 @@ private async Task<string> GetTranslationsImplementationAsync(string accessToken
}
}

private async Task<string> GenerateTranslationsImplementationAsync(string accessToken, ClientContext context, int? pageID, TranslationStatusCreationRequest translationStatusCreationRequest)
private async Task<string> GenerateTranslationsImplementationAsync(string accessToken, CookieContainer cookies, ClientContext context, int? pageID, TranslationStatusCreationRequest translationStatusCreationRequest)
{
await new SynchronizationContextRemover();

Expand Down Expand Up @@ -2474,6 +2479,10 @@ private async Task<string> GenerateTranslationsImplementationAsync(string access
{
handler.Credentials = networkCredential;
}
else
{
handler.CookieContainer = cookies;
}
}

request.Headers.Add("X-RequestDigest", await context.GetRequestDigestAsync());
Expand Down Expand Up @@ -2507,7 +2516,7 @@ private async Task<string> GenerateTranslationsImplementationAsync(string access
}


private async Task<string> GetClientSideWebPartsAsync(string accessToken, ClientContext context)
private async Task<string> GetClientSideWebPartsAsync(string accessToken, CookieContainer cookies, ClientContext context)
{
await new SynchronizationContextRemover();

Expand Down Expand Up @@ -2542,6 +2551,10 @@ private async Task<string> GetClientSideWebPartsAsync(string accessToken, Client
{
handler.Credentials = networkCredential;
}
else
{
handler.CookieContainer = this.cookies;
}
}

HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken());
Expand Down Expand Up @@ -2589,6 +2602,8 @@ private void Context_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
this.accessToken = e.WebRequestExecutor.RequestHeaders.Get("Authorization").Replace("Bearer ", "");
}

cookies = e.WebRequestExecutor.WebRequest.CookieContainer;
}
#endregion
}
Expand Down