尝试通过 WebClient 访问 SharePoint 列表中的文件时收到 403 响应

发布于 2024-10-12 03:32:10 字数 697 浏览 4 评论 0原文

我正在尝试使用 System.Net.WebClient 访问 SharePoint 列表中的文件。该列表已禁用匿名访问(当我们打开它时,它会起作用)并且我们正在使用基于声明的身份验证。我知道还有其他方法可以访问 SharePoint 列表中的文件,但是这是关于我对 Office Web Apps Web 服务进行的调用,在该调用中我必须传递文件的 URL,我希望它生成该文件的图像。使用此 URL 调用 OWA Web 服务并尝试通过 WebClient 直接下载文件都会产生相同的错误。

该错误是 403 禁止,经过一番谷歌搜索后,我相信原因在某种程度上与使用基于声明的身份验证有关。我已经尝试了一些建议的补救措施,但到目前为止没有任何效果。我可以使用浏览器访问该文件和 Web 服务,并且在收到身份验证质询后即可正常工作。如果我故意未通过身份验证质询,则会收到 401 错误(而不是 403),因此我不认为凭据有任何问题(我已经对它们进行了硬编码)。我尝试使用 RunWithElevated Privileges 运行代码,但这没有帮助。

这是一些示例代码:

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

感谢任何帮助!

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 tried 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-10-19 03:32:10

您可能必须使用 WebClient 进行声明登录,看看是否可以以此为起点。

    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 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);
            }
        }
    }
遮了一弯 2024-10-19 03:32:10

将 SharePoint OM 与 WIF 结合使用会带来更好的运气。
此处提供了一个示例:
http://www.shailen.sukul .org/2010/07/adfs-20-and-sharepoint-client-om.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文