locked
The request failed with HTTP status 401: Unauthorized. RRS feed

  • Question

  • Hi,

     

    I am trying to update the SharePoint list items via UpdateListItems web service. Initialy, I have tried to invoke the GetListCollection method via the following code:

     

    servername.Lists listservice = new servername.Lists();

     

    listservice.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

     

    System.Xml.XmlNode node = listservice.GetListCollection();

     

    foreach (System.Xml.XmlNode xmlnode in node)

    {

    Response.Write(xmlnode.Attributes["Title"].Value + "\n");

    }

     

    FYI -

    OS : Windows Server 2003, Enterprise Edition, SP2

     

    I am getting the following error:

     

     

    Server Error in '/ExploringSharePoint' Application.


    The request failed with HTTP status 401: Unauthorized.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Net.WebException: The request failed with HTTP status 401: Unauthorized.

    Source Error:

    Line 456:        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/GetListCollection", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    Line 457:        public System.Xml.XmlNode GetListCollection() {
    Line 458:            object[] results = this.Invoke("GetListCollection", new object[0]);
    Line 459:            return ((System.Xml.XmlNode)(results[0]));
    Line 460:        }

    Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\exploringsharepoint\03ff9d59\845f2ab2\App_WebReferences.gqzkarht.0.cs    Line: 458

    Stack Trace:

    [WebException: The request failed with HTTP status 401: Unauthorized.]
       System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +260
       System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +292
       chnmctlbiaps02.Lists.GetListCollection() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\exploringsharepoint\03ff9d59\845f2ab2\App_WebReferences.gqzkarht.0.cs:458
       Default2.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Madhu\My Documents\Visual Studio 2005\WebSites\ExploringSharePoint\Default2.aspx.cs:68
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
       System.Web.UI.Control.OnLoad(EventArgs e) +88
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3036
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832

     

    * I got to know that its because of some OS shortcoming via the following article:

     

    Knowledge Base 

     

    You may receive an "HTTP 401 Unauthorized" error message when you use SOAP over HTTP and Visual C# .NET XML Web services on a computer that is running Windows XP or Windows Server 2003Article ID: 885274

     

    Article Last Modified on 10/7/2005

    --------------------------------------------------------------------------------

    APPLIES TO

     

     

    Microsoft .NET Framework 1.1 Service Pack 1 (SP1)

     

    --------------------------------------------------------------------------------

     

    SYMPTOMS

    When you use SOAP over HTTP and Visual C# .NET XML Web services on a computer that is running Microsoft Windows XP or Microsoft Windows Server 2003, you may intermittently receive an error message that is similar to the following:

     

     

    System.Net.WebException: The request failed with HTTP status 401: Unauthorized.

     

     

    This problem occurs when a Web service uses Integrated Windows authentication, and the client passes the appropriate credentials correctly.

     

    Additionally, this problem may occur more frequently when multiple threads on the client try to connect to the network at the same time.

    CAUSE

    This issue occurs if a request time-out or an abort is not correctly handled.

    RESOLUTION

    Software update information

    A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Microsoft .NET 1.1 Framework service pack that contains this hotfix.

     

    Can anybody let me know what I need to do now to get the web service working?

     

    Thanks and Regards

    Madhu

    Wednesday, March 19, 2008 8:29 AM

Answers

  • It might be because of multiple hop scenario where your credentials are being passed from one machine to the other machine.

     

    Client machine -> Web Service machine -> SP machine

     

    As far as I know you need to enable Kerberos.

     

    Reply me if you need more information.

    Thursday, March 20, 2008 6:31 PM
  • Hi,

    Are You using Form Based Authentication? If so then you may need to use Authentication.asmx web service. If form based authentication then you may get an article on

    http://www.codeproject.com/KB/sharepoint/SharePointWSAccess.aspx


    Thanks
    Sohel Rana
    Sunday, March 23, 2008 12:13 PM

All replies

  • Madhu,

     

    Try

     

    listservice.UseDefaultCredentials = true;

    listservice.PreAuthenticate = true;

     

    instead of

     

    listservice.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

     

    If this doesn't work try...

     

    oServices.Credentials = new NetworkCredential(userName, password, domain);

     

    with some known credentials which will work.

     

    --Vince

    http://blog.thekid.me.uk

     

     

     

    Wednesday, March 19, 2008 9:14 AM
  • Hi Madhu!

    I'm facing the exact same problem, and haven't found a solution to it yet. I can't find a hotfix for .NET 2.0 which applies to this problem and I've tried the above mentioned settings, but with no affect. Have you come up with a solution?

    Regards
    David
    Wednesday, March 19, 2008 3:49 PM
  • It might be because of multiple hop scenario where your credentials are being passed from one machine to the other machine.

     

    Client machine -> Web Service machine -> SP machine

     

    As far as I know you need to enable Kerberos.

     

    Reply me if you need more information.

    Thursday, March 20, 2008 6:31 PM
  • Hi,

    Are You using Form Based Authentication? If so then you may need to use Authentication.asmx web service. If form based authentication then you may get an article on

    http://www.codeproject.com/KB/sharepoint/SharePointWSAccess.aspx


    Thanks
    Sohel Rana
    Sunday, March 23, 2008 12:13 PM
  • Try http://support.microsoft.com/default.aspx?scid=kb;en-us;896861 since Windows will protect itself where Machine name and web site FQDN do not match. You may also get some use out of http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/a93e5074-a06f-46e2-af82-c743a9ac96b4/ and there are two variants of the NetworkCredential constructor depending upon authentication type.

    Here is a code sample of accessing a list which does not always require the SharePoint authentication service to be invoked beforehand.

                     ListsProxy.Lists objListsProxy = new ListsProxy.Lists();
                    objListsProxy.Url = @"http://YourSharePoint/_vti_bin/lists.asmx";
                    objListsProxy.Credentials = new NetworkCredential("Administrator", "mypassword");
                    System.Xml.XmlNode xmlListCollection = objListsProxy.GetListCollection();

    If the above does not work, there is a more comprehensive set of troubleshooting steps at: 
    http://www.pentalogic.net/sharepoint-reminder/manual.aspx?Page=troubleshooting/401errors/401errors.htm

    • Proposed as answer by Andrew Maisey Friday, February 13, 2009 5:04 PM
    Friday, February 13, 2009 5:04 PM
  • Hi Madhu,

    Were you able to find the solution? I am also facing a similar issue. I know you have posted this in march 2008, but just giving it a try as i can't find the solution anywhere.  Please help me with this if possible.

    Wednesday, July 15, 2009 7:21 AM
  • Hi Ukri,

    I was into the same problem, thought of posting a reply.

    I was using sharepoint authentication web service initially but was not getting authenticated and henceforth i was not able to use the cookie returned for my list web service. Therefore i ignored the authentication web service call completely and relied on the list webservice alone by using network credentials.

    ListsWS.Lists aceService = new ActivityTracking.ListsWS.Lists();
    aceService.Credentials = new NetworkCredential(strUsername, strPassword, strDomain);
    return aceService.GetListItems(ListName, null, ndQuery, ndViewFields, null, ndQueryOptions, null);

    This proved to work flawless.
    Tuesday, October 20, 2009 7:08 PM
  • I was not having a problem with GetListItems until they tried added SSL certificate to our site.  All of a sudden I can no longer get my calls to GetListItems to authenticate if I deploy the code to the SharePoint server.  If I run the code on my local machine and call the remote GetListItems it works just fine.  We are running SharePoint on a Windows 2003 server with .NET 3.5 installed.  My code looks like this:

    myccbc.

    Lists listService = new myccbc.Lists();

    listService.Credentials = System.Net.

    CredentialCache.DefaultCredentials;

    listService.Url =

    "http://<server>/_vti_bin/Lists.asmx";

    System.Xml.

    XmlDocument xmlDoc = new System.Xml.XmlDocument();

     

    string listName = "{B2586A81-3120-4DDA-9C9B-9E512E4B30AB}";

     

    string viewName = "{93EDE906-852B-4BE2-897B-0DC3F4AD1519}";

     

    string rowLimit = "200";

    System.Xml.

    XmlElement query = xmlDoc.CreateElement("Query");

    System.Xml.

    XmlElement viewFields = xmlDoc.CreateElement("ViewFields");

    System.Xml.

    XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

    query.InnerXml =

    "<OrderBy><FieldRef Name='EventDate' Ascending='True' /></OrderBy>";

    viewFields.InnerXml =

    "<FieldRef Name=\"Title\" /><FieldRef Name=\"Description\" /><FieldRef Name=\"Location\" />" +

     

    "<FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" />" +

     

    "<FieldRef Name=\"EventType0\" /><FieldRef Name=\"fAllDayEvent\" />";

    queryOptions.InnerXml =

    "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";

    System.Xml.

    XmlNode nodeListItems =

    listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions,

    null);

    Friday, November 6, 2009 1:10 PM
  • Hi jkelch,

    I think your issue is not related to the original post (The request failed with HTTP status 401: Unauthorized). Or do you get that HTTP status code back?

    I suggest you to start a new thread, and I try to figure out the reason in the meantime.

    Peter
    Friday, November 6, 2009 1:21 PM
  • Peter,

    Yes, I do get the 401: Unauthorized status code.

    Joe
    Friday, November 6, 2009 1:44 PM
  • Hi Joe,

    Then it means your code runs remotely while it gives you an error when you try on the server? Do you use the same URL and does code run with the same credentials (user, password)? Does the site have host headers configured for?

    Please, be sure that this behaviour was not caused by the so-called Authentication Loopback Check method introduced in Windows Server 2003 SP1. You can read more about that here:
    You receive error 401.1 when you browse a Web site that uses Integrated Authentication and is hosted on IIS 5.1 or a later version
    http://support.microsoft.com/default.aspx/kb/896861

    Peter
    Friday, November 6, 2009 3:09 PM
  • Thanks Peter, I will look into this.  Would this be something that could be triggered by the deployment of an SSL cert?
    Friday, November 6, 2009 5:06 PM
  • Hi Joe,

    "Would this be something that could be triggered by the deployment of an SSL cert?"

    I don't think deploying an SSL certificate has some relations with the loopback check. But it is worth giving a try.

    Peter

    Friday, November 6, 2009 6:27 PM
  • Hi Vince,

     

    Thanks for the line

    Try

     

    listservice.UseDefaultCredentials = true;

    listservice.PreAuthenticate = true; These lines helped me solve the problem discussed in the forum. Learning Sharepoint 2010 and this is a point to remember. Mo


    Mohawk
    Sunday, November 27, 2011 5:55 PM
  • Great Sir......... It Solved my Problem,

    Thankyou


    Muhammad Ali

    Friday, April 20, 2012 9:45 AM
  • "new NetworkCredential("username", "password") is coming up red, undefined.
    Is it in a library? Is there a service one has to add?
    Friday, June 15, 2012 7:06 PM
  • try new System.Net.NetworkCredential("username","Password","Domain");
    Friday, July 27, 2012 9:27 AM
  • Thanks

    oServices.Credentials = new NetworkCredential(userName, password, domain);

    this one is worked for me on windows 2008 server....

    Friday, January 4, 2013 9:27 AM