我现在正在解决一个问题,它涉及从我们的局域网SharePoint服务器下载多个PDF文件到我们的DMZ。
我们试图实现的是一个"NewsFeed“类型的应用程序。它所做的是从SharePoint上传的内容类型中获取列表数据,然后将它们发布到我们托管在非军事区的外部网站上,并提供适当的pdf文件的链接。我们使用sharepoint提供的REST API轻松地完成了文本布局,但现在遇到的问题是将上传文档的.pdf从SharePoint站点复制到DMZ,这样我们就可以将它们存储在缓存中,用户可以通过href访问它们。
下面是用于访问SharePoint REST API的代码。
// Create the connection to sharepoint. Credentials are managed within web.config
SharePointListsREST.SharePointDataContext dc = new SharePointListsREST.SharePointDataContext(new Uri(ConfigurationManager.AppSettings["spUrl"]));
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTLM", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
dc.Credentials = cc;这是完美的,它能够进行身份验证,获取数据并显示它。对于PDF下载功能,我们有这样的功能。
private void GetPDF(string URL, string path)
{
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
WebClient wc = new WebClient();
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTML", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
wc.Credentials = cc;
wc.DownloadFile(URL, path);
} 这根本不起作用,并抛出一个401未经授权的error.However,如果您尝试通过转到http://XX.XX.XX.XX/.../test.pdf访问pdf文件,将提示您输入用户名/密码。输入基于windows的凭据将使我能够访问pdf。
我试过使用wc.UseDefaultCredentials = true;,但它也不起作用。有没有人有关于如何让它与get客户端一起工作的想法?
以下是IIS中的一个错误日志。
2012-07-11 00:56:12 XX.XX.XX.XX GET /Employee+Images/_t/sample.jpg - 80 - 192.168.200.12 - 401 2 5 0发布于 2012-07-12 02:51:37
在SharePoint站点上使用Windows auth时,可以选择模拟用户并在模拟块中运行整个WebClient代码。
有关模拟用户,请参阅此问题How can I use Directory.CreateDirectory with a different user id?。
或者,您可以简单地以Runas的特殊用户身份运行整个过程-不需要更改代码。
https://stackoverflow.com/questions/11439248
复制相似问题