开发者

Getting 403 response when trying to access file in SharePoint list via WebClient

开发者 https://www.devze.com 2023-02-04 03:28 出处:网络
I’m trying to access a file in a SharePoint list using System.Net.WebClient. The list has anonymous access disabled (when we turn it on, it works) and we’re using Claims-Based Authentication. I know

I’m trying to access a file in a SharePoint list using System.Net.WebClient. The list has anonymous access disabled (when we turn it on, it works) and we’re using Claims-Based Authentication. I know that there are other ways of accessing a file in a SharePoint list, however this is regarding a call that I make to an Office Web Apps web service where I have to pass the URL of a file I want it to generate an image of. Both calling the OWA web service with this URL and trying to download the file directly via WebClient yield the same error.

The error is 403 forbidden, which after some googling I believe the cause is somehow related to using Claims-Based Authentication. I’ve tried a number of the suggested remedies, but so far nothing has worked. I can access the file and the web service using a browser and it works after I get an authentication challenge. If I fail the authentication challenge intentionally I get a 401 error (not a 403), so I don’t believe there’s anything wrong with the credentials (I’ve gone so far as to hard-code them). I’ve 开发者_如何学Ctried running the code with RunWithElevated Privileges, but that doesn’t help.

Here is some sample code:

    using (var webClient = new WebClient())
    {
    webClient.UseDefaultCredentials = true;
    byte[] result = webClient.DownloadData(urlOfFileInList);
    }

Any help is appreciated!


You might have to do the claims login with WebClient, see if you can use this as a starting point.

    using (var webClient = new WebClient()) {
        string url = "http://yoursite";
        string result = null;
        try {
            result = webClient.DownloadString(url);
        } catch (Exception ex) {
            if (ex.ToString().Contains("403")) {
                result = webClient.DownloadString(url + "/_forms/default.aspx");
                string viewstate = result.Substring(result.IndexOf("__VIEWSTATE") + 11);
                viewstate = viewstate.Substring(viewstate.IndexOf("value=\"") + 7);
                viewstate = viewstate.Substring(0, viewstate.IndexOf("\""));
                string eventvalidation = result.Substring(result.IndexOf("__EVENTVALIDATION") + 17);
                eventvalidation = eventvalidation.Substring(eventvalidation.IndexOf("value=\"") + 7);
                eventvalidation = eventvalidation.Substring(0, eventvalidation.IndexOf("\""));
                System.Collections.Specialized.NameValueCollection values = new System.Collections.Specialized.NameValueCollection();
                values.Add("__EVENTARGUMENT", "");
                values.Add("__EVENTTARGET", "");
                values.Add("__EVENTVALIDATION", eventvalidation);
                values.Add("__LASTFOCUS", viewstate);
                values.Add("__VIEWSTATE", "");
                values.Add("ctl00$PlaceHolderMain$signInControl$UserName", "");
                values.Add("ctl00$PlaceHolderMain$signInControl$login", "Sign In");
                values.Add("ctl00$PlaceHolderMain$signInControl$password", "");
                //byte[] data = webClient.UploadValues(url + "/_forms/default.aspx", "POST", values);
                //result = System.Text.Encoding.Default.GetString(data);
                //result = webClient.UploadString(url + "/_forms/default.aspx?__EVENTARGUMENT=&__EVENTTARGET=&__EVENTVALIDATION=" + 
                //    eventvalidation + "&__LASTFOCUS=&__VIEWSTATE=" + viewstate + 
                //    "&ctl00$PlaceHolderMain$signInControl$UserName=&ctl00$PlaceHolderMain$signInControl$login=Sign+In&ctl00$PlaceHolderMain$signInControl$password=", "");
                string location = webClient.ResponseHeaders["Location"];
                result = webClient.DownloadString(url);
            }
        }
    }


You will have better luck using the SharePoint OM with WIF. An example is available here: http://www.shailen.sukul.org/2010/07/adfs-20-and-sharepoint-client-om.html

0

精彩评论

暂无评论...
验证码 换一张
取 消