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

Hello, I hope you can help me. All Ive been trying to do for the last month is set up a test Web Service but Ive seemed to have run into problems every step of the way; from hardware set ups to authentication setting etc.

Okay, I have a Web Service (TestWebService in IIS) that makes a connection to SQL Database. I also have an ASP.Net consumer page (TestWebServiceConsumer in IIS) that displays the content of the database in a GridView on a Button event. Both work fine by themselves, both are set to use ASP.net 2, Annoymonous: IUSR & Intergreated Windows Authentication. Both are placed the the Default Web Site and have seperate Address Pools. They also have full access to users, IUSR, Network Service, IIS_WPG etc.

When I run the consumer http://IPAddress/TestWebServiceConsumer/Default.aspx to retrieve http://IP Address/TestWebService/Music.asmx I get the following error message...

+----------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------+

Server Error in '/TestWebServiceConsumer' 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 45:         [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://IPAddress/TestWebService/Music.asmx", RequestNamespace = "http://10.10.10.86/TestWebService/Music.asmx", ResponseNamespace = "http://10.10.10.86/TestWebService/Music.asmx", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
Line 46:         public System.Data.DataSet GetProduct() {
Line 47:             object[] results = this.Invoke("GetProduct", new object[0]);
Line 48:             return ((System.Data.DataSet)(results[0]));
Line 49:         }

Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\testwebserviceconsumer\94c96322\e12d9ffe\App_WebReferences.wqch4y0d.0.cs    Line: 47

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) +551137
   System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +204
   WebReference.Music.GetProduct() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\testwebserviceconsumer\94c96322\e12d9ffe\App_WebReferences.wqch4y0d.0.cs:47
   _Default.Button1_Click(Object sender, EventArgs e) in c:\WebSites\WebSite_WEBConsumer\Default.aspx.cs:26
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746


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

+--------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+

[x]Code: Web Service...

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Music
/// </summary>
[WebService(Namespace = "http://IP Address/TestWebService/Music.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Music : System.Web.Services.WebService {

    public Music () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(CacheDuration = 30, Description = "This WebService returns the Customer table in the Northwind Database.")]
    
    public DataSet GetProduct() 
    {
        SqlConnection conn;
        SqlDataAdapter myDataAdapter;
        DataSet myDataSet;
        const string cmdString = "Select * From Customers";

        conn = new SqlConnection("Server=lrd-sbs;Uid=michael;Pwd=kearney;Database=Northwind");
        myDataAdapter = new SqlDataAdapter(cmdString, conn);

        myDataSet = new DataSet();
        myDataAdapter.Fill(myDataSet, "Customers");

        return myDataSet;
    }
    
}
 
[x] Code: Web Consumer...
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page 
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        WebReference.Music nw = new WebReference.Music();
        GridView1.DataSource = nw.GetProduct();
        GridView1.DataBind();
    }
}
+---------------------------------------------------------------------------------+--------------------------------------------------------------------+

 I found this site but it doesnt really make too much sense, Im not sure where to insert the code or how to customise it to my: http://heybo.com/weblog/posts/245.aspx

WebService.webClass myWC= new WebService.webClass();

myWC.PreAuthenticate = true;

myWC.Credentials = System.Net.CredentialCache.DefaultCredentials;

 

I tried placing it inside the Button event before the rest of my code. I got an error message...

The type name 'Music' does not exist in the type 'System.Web.Services.WebService'

Thank you for your help!

 Mike

0
mikexkearney
8/12/2008 10:06:23 AM
asp.net.xml-web-services 7071 articles. 0 followers. Follow

5 Replies
2523 Views

Similar Articles

[PageSpeed] 24

TestWebService needs Authentication.

go to property of TestWebService, select enable anonymouse access and also select integrated windows authentication.


Imran
[MCTS]
Few have audacity to speak truth
http://knowledgebaseworld.blogspot.com/
0
bhadelia
8/12/2008 11:29:26 AM

"Both work fine by themselves, both are set to use ASP.net 2, Annoymonous: IUSR & Intergreated Windows Authentication."

 

Both have "Enable Anonymous Access" ticked with IUSER as the profile.

Both have only "Intergrated Windows Authentication" ticked.

 

Can anybody help me with that code please?

0
mikexkearney
8/12/2008 12:27:41 PM

!!!Sorted!!!

 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    WebReference.Music nw = new WebReference.Music(); 
    nw.PreAuthenticate = true; 
    nw.Credentials = System.Net.CredentialCache.DefaultCredentials; 
    GridView1.DataSource = nw.GetProduct(); 
    GridView1.DataBind(); 
}
 
  
0
mikexkearney
8/13/2008 10:36:21 AM

Hi All,

I'm sorry I might sound stupid to ask this question. But can anyone help to out in explaining what is  these two line of code for?

    nw.PreAuthenticate = true;
    nw.Credentials = System.Net.CredentialCache.DefaultCredentials;
in mikeskearney's reply.

 Thank you. 

mikexkearney:

!!!Sorted!!!

 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    WebReference.Music nw = new WebReference.Music(); 
    nw.PreAuthenticate = true; 
    nw.Credentials = System.Net.CredentialCache.DefaultCredentials; 
    GridView1.DataSource = nw.GetProduct(); 
    GridView1.DataBind(); 
}
 

 

0
weoili
1/21/2009 7:12:33 AM

By passing the default credentials, the credentials of the ASP.NET page will be used as the credentials to access the web service.


John Saunders | MVP – Connected System Developer
0
johnwsaunders3
1/21/2009 8:30:39 PM
Reply: