none
WebException: The remote server returned an error: (401) Unauthorized - Sharepoint RRS feed

  • Question

  • Hi

    I am trying to access a document in Sharepoint Server through asp. When i am getting the response, this errror is thrown.

    WebException: The remote server returned an error: (401) Unauthorized.]
       System.Net.HttpWebRequest.GetResponse() +1501995

    My piece of code:

    Uri uri = new Uri("http://<Server_Name>/Shared Documents/schema.xls");

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

    req.Credentials = CredentialCache.DefaultNetworkCredentials;

    Response.Write(req.Credentials);

    Response.Write(req.HaveResponse);

    //req.PreAuthenticate = false;

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();/// <<<<<<<<<< ERROR LINE

    I have enabled anonymous user on my sharepoint portal.

    I have kerberos as IIS authentication.

    I have enabled Client interaction.

    Please let me know about this. If there are some other settings which i need to change. If i run the same piece of code from stand alone(console based application) it returns me the response.

    Do i need to add the anonymous user account in some Group on the IIS server machine ?

    Thanks,

    Vipul

     

     

    Monday, February 12, 2007 5:35 PM

Answers

  • Hi

    i had same problem and got it solved...follow this

    1. Open IIS manager (Start | Control Panel | Administrative Tools | Internet Information Services Manager).
        2. Expand the “Application Pools” node.
        3. Right click the application pool which your project is using, and then select “Properties”.
        4. Click “Identity” tab.
        5. Choose “Local System” in the Predefined dropdown list.


    About how to grant permission to a file, please check these steps:

        1. Open Windows Explorer.
        2. Right click the file, and then select "Properties".
        3. Click the "Security" tab.
        4. Add "Network Service" in access list and check "Modify", "Read", and "Write" for it.

    and finally

    in your code add this

    System.Net.WebRequest request = System.Net.HttpWebRequest.Create("http://localhost/Webservicetest1/Service.asmx/webmrthod");

    request.UseDefaultCredentials = true;

    request.PreAuthenticate = true;

    request.Credentials = CredentialCache.DefaultCredentials;

    System.Net.WebResponse response = request.GetResponse();

    it is working without any problem

    Wednesday, June 3, 2009 7:14 AM
  • worked by only adding the lines of code :

    request.UseDefaultCredentials = true;

    request.PreAuthenticate = true;

    request.Credentials = CredentialCache.DefaultCredentials;

    Tuesday, October 4, 2011 7:22 PM

All replies

  •  I have this problem so,

    For authenticate< I used these line but still error (401) Unauthorized)

    webRequest.PreAuthenticate = true;

    webRequest.Credentials = new NetworkCredential("xx", "yy", "zz");

     

     


    SharePoint/.Net developer Reza Nazemi
    • Proposed as answer by Reza Nazemi Friday, January 30, 2009 2:27 AM
    • Unproposed as answer by Mike Walsh FIN Friday, February 6, 2009 6:06 AM
    Tuesday, January 27, 2009 2:43 AM
  • Hi

    i had same problem and got it solved...follow this

    1. Open IIS manager (Start | Control Panel | Administrative Tools | Internet Information Services Manager).
        2. Expand the “Application Pools” node.
        3. Right click the application pool which your project is using, and then select “Properties”.
        4. Click “Identity” tab.
        5. Choose “Local System” in the Predefined dropdown list.


    About how to grant permission to a file, please check these steps:

        1. Open Windows Explorer.
        2. Right click the file, and then select "Properties".
        3. Click the "Security" tab.
        4. Add "Network Service" in access list and check "Modify", "Read", and "Write" for it.

    and finally

    in your code add this

    System.Net.WebRequest request = System.Net.HttpWebRequest.Create("http://localhost/Webservicetest1/Service.asmx/webmrthod");

    request.UseDefaultCredentials = true;

    request.PreAuthenticate = true;

    request.Credentials = CredentialCache.DefaultCredentials;

    System.Net.WebResponse response = request.GetResponse();

    it is working without any problem

    Wednesday, June 3, 2009 7:14 AM
  • Work like a charm! thanks for sharing.

    Wednesday, June 29, 2011 4:09 PM
  • Thanks, its working fine .. :-) 
    Tuesday, September 27, 2011 5:14 AM
  • worked by only adding the lines of code :

    request.UseDefaultCredentials = true;

    request.PreAuthenticate = true;

    request.Credentials = CredentialCache.DefaultCredentials;

    Tuesday, October 4, 2011 7:22 PM
  • Can you please tell me, where we need to add the codes.

    Thursday, October 13, 2011 12:12 AM
  • Worked like a charm.

    But later I removed "grant permission to a file" step. After that also it worked fine for me.

    • Edited by Co Der Thursday, November 3, 2011 9:37 AM Wanted to edit
    Thursday, November 3, 2011 9:20 AM
  • worked by only adding the lines of code :

    request.UseDefaultCredentials = true;

    request.PreAuthenticate = true;

    request.Credentials = CredentialCache.DefaultCredentials;

    ???
    Friday, September 4, 2015 12:00 PM
  • I get the same 401 unauthorised error when trying to use the "update list item" activity in Orchestrator to connect to a SharePoint list and trying to update a value. Do you have an idea on this?

    Thanks,

    Bindushree L


    Friday, January 29, 2016 6:23 PM
  • Sirs.

    Where I found de code?

    Thanks!

    Thiago

    Thursday, February 16, 2017 12:50 PM
  • yes please, i dont know where i need to add this code.
    Monday, July 17, 2017 8:11 PM