如何阅读特定版本的文件内容?
我试过了:
var webClient = new WebClient() {Credentials = Context.Credentials })
webClient.DownloadString($"{Context.Url}{fileVersion.Url}");
但Credentials
为空。我也试过UseDefaultCredential = true
,但在这两种情况下我都禁止403:
System.Net.WebException:'远程服务器返回错误:(403)Forbidden。'
我也尝试过:
File.OpenBinaryDirect(sharepoint.Context, fileVersion.Url)
//fileVersion.Url == "_vti_history/512/ListName/filename.xml"
> Specified argument was out of the range of valid values.
> Parameter name: serverRelativeUrl
并以/
开头:
File.OpenBinaryDirect(sharepoint.Context, "/" + fileVersion.Url)
System.Net.WebException:'远程服务器返回错误:(404)Not Found。'
我使用sharepoint PnP来创建和验证我的ClientContext:
var authentication = new OfficeDevPnP.Core.AuthenticationManager();
Context = authentication.GetWebLoginClientContext(siteUrl);
我能够检索FileVersion列表:
var items = list.GetItems(camlQuery);
Context.Load(items);
Context.ExecuteQuery();
foreach (ListItem item in items)
{
sharepoint.Context.Load(item.Versions);
sharepoint.Context.Load(item.File.Versions);
sharepoint.Context.Load(item.File);
sharepoint.Context.ExecuteQuery();
foreach (var fileVersion in item.File.Versions)
{
ReadFile(fileVersion);
}
}
这是我的库的版本:
<package id="Microsoft.SharePointOnline.CSOM" version="16.1.7018.1200" targetFramework="net461" />
<package id="SharePointPnP.IdentityModel.Extensions" version="1.2.2" targetFramework="net461" />
<package id="SharePointPnPCoreOnline" version="2.19.1710.2" targetFramework="net461" />
答案 0 :(得分:1)
我通过从ClientContext窃取cookie来攻击webclient身份验证:
var authentication = new OfficeDevPnP.Core.AuthenticationManager();
context = authentication.GetWebLoginClientContext(siteUrl);
webClient = new WebClient();
context.ExecutingWebRequest += StealCookieOnExecutingWebRequest;
void StealCookieOnExecutingWebRequest(object sender, WebRequestEventArgs e)
{
var cookies = e.WebRequestExecutor.WebRequest.CookieContainer;
webClient.Headers[HttpRequestHeader.Cookie] = cookies.GetCookieHeader(new Uri(SiteUrl));
context.ExecutingWebRequest -= StealCookieOnExecutingWebRequest;
}
我不会将此标记为答案,因为可能有更好的解决方案。