1

I am working on a problem right now and it involves downloading multiple PDF files to our DMZ from our LAN SharePoint server.

What we are trying to accomplish is a "NewsFeed" type application. What it does is take list data from uploaded Content Types made in SharePoint, then post them to our external website hosted on the DMZ, with a link to the appropriate pdf file. We used the REST API that sharepoint has to accomplish the text layout fairly easily, but the problem we are now running into is copying the .pdf of the uploaded document from the SharePoint site, onto the DMZ, so we can store them in a cache, and the user can access them through an href.

Here is code that works, to access the 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;

This works perfectly, it is able to authenticate, grab the data and display it. For the PDF download functionality we have this.

    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);
    } 

which does not work at all and throws a 401 Not Authorized error.However, if you go to try to access the pdf file by going to http://XX.XX.XX.XX/.../test.pdf you will be prompted for a username/password. Entering my windows based credentials will give me access to the pdf.

I have tried using wc.UseDefaultCredentials = true; but it does not work either. Does anyone have any ideas on how I can get this to work with the webclient?

Here is one of the error logs from 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
Mike Chester
  • 98
  • 1
  • 8

1 Answers1

0

Impersonating the user and running whole WebClient code inside impersonated block is an option when you use Windows auth on SharePoint site.

See this question How can I use Directory.CreateDirectory with a different user id? on impersonating a user.

Or you simply can run whole process as that special user with Runas - no code changes would be needed.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Hmm, we tried setting impersonation up in the web.config, but we still come back with the same error message. After some clarification with my Manager, we are running IIS 7 and the error that we get in the logs is 401.2 (I think). – Mike Chester Jul 11 '12 at 20:31