以编程方式从基于声明身份验证的 SharePoint 2010 网站下载文件

发布于 2024-09-05 15:37:25 字数 433 浏览 11 评论 0原文

我有一个控制台应用程序用于从 SharePoint 站点下载文件。 Sharepoint 站点使用基于声明的身份验证。

此代码引发 403 Forbidden 异常。指定的网络凭据具有对该站点的完全访问权限,并且能够从浏览器下载相同的文件。

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(username,Password,domain);
byte[] fileData = webClient.DownloadData(urlOfAFile); 
FileStream file = File.Create(localPath);
file.Write(fileData, 0, fileData.Length);

知道如何解决这个问题吗?

I have a console application to download a file from a SharePoint site. The sharepoint site uses claims based authentication.

This code throws a 403 Forbidden exception. The specified Network credential has full access to the site, and is able to download the same file from a browser.

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(username,Password,domain);
byte[] fileData = webClient.DownloadData(urlOfAFile); 
FileStream file = File.Create(localPath);
file.Write(fileData, 0, fileData.Length);

Any idea how to fix this?

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

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

发布评论

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

评论(2

财迷小姐 2024-09-12 15:37:25

也许有点晚了,但是在发出请求之前添加正确的请求标头可以解决问题:

webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

Maybe a bit late, but adding the right request header before making the request solves the problem:

webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
微凉徒眸意 2024-09-12 15:37:25

我也遇到了这个问题,下面是我的研究:

{
  ClientContext m_clientContext = new ClientContext(strSiteUrl);
    m_clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
    m_clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
    m_clientContext.Credentials = new NetworkCredential(uname, pwd);
    Web m_currentWeb = m_clientContext.Web;
    m_clientContext.Load(m_currentWeb);
    m_clientContext.ExecuteQuery();
}

  private void ctx_MixedAuthRequest(object sender, WebRequestEventArgs e)
    {
        try
        {
            //Add the header that tells SharePoint to use Windows authentication.
            e.WebRequestExecutor.RequestHeaders.Add(
            "X-FORMS_BASED_AUTH_ACCEPTED", "f");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error setting authentication header: " + ex.Message);
        }
    }

这是文章: https://msdn.microsoft.com/en-us/library/office/hh124553(v=office.14).aspx

I also encounter this issue, and below is my research:

{
  ClientContext m_clientContext = new ClientContext(strSiteUrl);
    m_clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
    m_clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
    m_clientContext.Credentials = new NetworkCredential(uname, pwd);
    Web m_currentWeb = m_clientContext.Web;
    m_clientContext.Load(m_currentWeb);
    m_clientContext.ExecuteQuery();
}

  private void ctx_MixedAuthRequest(object sender, WebRequestEventArgs e)
    {
        try
        {
            //Add the header that tells SharePoint to use Windows authentication.
            e.WebRequestExecutor.RequestHeaders.Add(
            "X-FORMS_BASED_AUTH_ACCEPTED", "f");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error setting authentication header: " + ex.Message);
        }
    }

here is the article: https://msdn.microsoft.com/en-us/library/office/hh124553(v=office.14).aspx

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