Download a file from a Claims auth based SharePoint 2010 site programmatically

10,259

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");
Share:
10,259
turnip_cyberveggie
Author by

turnip_cyberveggie

Polyglot programmer from Bangalore, India

Updated on June 26, 2022

Comments

  • turnip_cyberveggie
    turnip_cyberveggie almost 2 years

    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?

    • Mikael Svenson
      Mikael Svenson almost 14 years
      Is the site set to require ssl (check directoy security in IIS)
    • turnip_cyberveggie
      turnip_cyberveggie almost 14 years
      Yes... The site is configured to use HTTPS
    • Admin
      Admin over 13 years
      Any luck with this problem? I am running into a similar issue.
  • turnip_cyberveggie
    turnip_cyberveggie about 13 years
    Hi, Thanks for answering. After adding your code snipped, I am getting a 401 Exception. Earlier, I was getting a 403 exception. Am I missing anything?
  • Mel Gerats
    Mel Gerats about 13 years
    Are you trying to log in with Windows authentication or Forms authentication?
  • nixjojo
    nixjojo about 12 years
    Hi @ashwnacharya Have you solved this problem? I also have the same problem, if you know how to solve it, can you post your answer here? Thanks!
  • Vijay
    Vijay over 11 years
    This solution worked for me! I tried the other solution provided in the same forum which did not work for me.
  • EthR
    EthR over 7 years
    I had to add the user agent header also and then it worked. _SPWebClient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";