"401 Not Authorized" error when authenticating a web service

Last post 08-16-2007 5:12 PM by BigBlueEye. 9 replies.

Sort Posts:

  • "401 Not Authorized" error when authenticating a web service

    12-01-2006, 3:23 PM
    • Loading...
    • Afro Blanca
    • Joined on 09-14-2004, 4:01 PM
    • NYC, baby!
    • Posts 100
    Here's the situation :

    I have the same asp.net 2.0 web application running on both Machine A and Machine B.  On both machines, I have Integrated Windows Authentication turned on, and Anonymous Access turned off.  When I hit a certain page on Machine A, it will call the web service on Machine A, and then it will call the same web service on Machine B.  Likewise, when I hit the same page on Machine B, it will call the web service on Machine B, and then it will call the same web service on Machine A.

    On both machines, I have an account set up for this web service to use.  The username/password for Machine A is different from the username/password for Machine B.  The machines are on different Active Directory domains.

    The websites on Machine A and Machine B use the same second-level domain, but have different subdomains.

    On the page that calls the webservice, I have a block of code that looks like this :

    try
    {//call the webservice on Machine A
        CacheMaintenance maintain = new CacheMaintenance("local.mysite.com");
        maintain.Credentials = new NetworkCredential("LocalLoginName", "LocalPassword", "LocalDomain");
        maintain.FlashTheCache();
        maintain.Dispose();
    }
    catch (Exception exp)
    {
        errors = "Could not flash local cache.  " + General.UnrollException(exp) + "  ";
    }

    try
    {//call the webservice on Machine B
        CacheMaintenance maintain = new CacheMaintenance("testlive.mysite.com");            
        maintain.Credentials = new NetworkCredential("TestLiveLoginName", "TestLivePassword", "TestLiveDomain");            
        maintain.FlashTheCache();
        maintain.Dispose();
    }
    catch (Exception exp)
    {
        errors += "Could not flash live cache.  " + General.UnrollException(exp);
    }   

    Machine A has no problem calling the webservice on Machine B.  Likewise Machine B has no problem calling the webservice on Machine A.  However, when Machine A tries to call the webservice on Machine A, or when Machine B tries to call the webservice on Machine B, I get a "401 Not Authorized" error.

    Does anybody know why I'm getting this error?

    Thanks for your help.
  • Re: "401 Not Authorized" error when authenticating a web service

    12-02-2006, 3:04 PM
    • Loading...
    • Afro Blanca
    • Joined on 09-14-2004, 4:01 PM
    • NYC, baby!
    • Posts 100
    Here's a minor correction to my original post :

    Originally, I said that Machine A could call the webservice on Machine B but not on Machine A, and that Machine B could call the webservice on Machine A but not on
    Machine B.  After some more testing, I realized this was not the case.

    Machine A is a Windows 2000 Server machine.  It is capable of calling the webservice on Machine A and on Machine B.

    Machine B is a Windows 2003 Server machine.  It is capable of calling the webservice on Machine A, but not Machine B.

    Here are some things I tried :

    I tried having Machine B call the webservice on Machine B using CredentialCache.DefaultCredentials.
    I tried having Machine B call the webservice on Machine B using CredentialCache.DefaultNetworkCredentials.
    I tried having Machine B call the webservice on Machine B using the administrator account credentials.
    I tried having Machine B call the webservice on Machine B without setting the credentials at all.

    All to no avail.  This seems strange to me.  Why would Machine A be able to call the webservice on Machine B, but Machine B couldn't call the webservice on Machine B with the very same credentials?
  • Re: "401 Not Authorized" error when authenticating a web service

    01-23-2007, 4:08 PM
    • Loading...
    • megrogan
    • Joined on 11-09-2005, 7:27 PM
    • Posts 1

    I have exactly the same problem. The only difference is that for me machine A is running Windows XP Pro, B is Windows 2003 Server.

    A little research suggested that having the WebService impersonate the client might be the key but I've not tried yet.

    Any luck finding a solution? Has anyone else come across this?

     

  • Re: "401 Not Authorized" error when authenticating a web service

    01-23-2007, 4:26 PM
    • Loading...
    • Afro Blanca
    • Joined on 09-14-2004, 4:01 PM
    • NYC, baby!
    • Posts 100

    Heh, no luck finding a solution yet.  However, I did upgrade Machine A from Windows Server 2000 to Windows Server 2003.  The result is that, like Machine B, it can no longer call the webservice on itself.  So now we know that :

    1) This is a problem with Win2K3 in general, and not just Machine B's specific setup.

    2) This is a problem that wasn't present in Win2K.

    My guess is that some part of the Windows security model changed between 2K and 2K3.

    I kinda gave up on this for a while, but would really like to find a solution.  I was figuring I'd re-post the question to a few forums some time next week.
     

  • Re: "401 Not Authorized" error when authenticating a web service

    01-25-2007, 12:11 PM
    • Loading...
    • scommisso
    • Joined on 07-02-2004, 12:11 AM
    • Phoenix, AZ, USA
    • Posts 139

    That's a strange problem. This may or may not help, but try this. Instead of Integrated Windows Authentication, swich to Digest authentication and enter your AD domain i nthe configuration. Then switch your credential code to the following:

    Net.NetworkCredential userCredential = New Net.NetworkCredential("Username", "Password", "Domain");
    Net.CredentialCache credentials = new Net.CredentialCache();
    credentials.Add("http://local.mysite.com/wsDirName", "Digest", userCredential);
    maintain.Credentials = credentials;
    maintain.PreAuthenticate = true;
    Hope this helps.
    Steve Commisso
    MCSD.NET, MCPD: EAD
  • Re: "401 Not Authorized" error when authenticating a web service

    01-25-2007, 12:14 PM
    • Loading...
    • scommisso
    • Joined on 07-02-2004, 12:11 AM
    • Phoenix, AZ, USA
    • Posts 139

    Oops, left something out of the code... first credential argument should have been a URI:

    Net.NetworkCredential userCredential = New Net.NetworkCredential("Username", "Password", "Domain");
    Net.CredentialCache credentials = new Net.CredentialCache();
    credentials.Add(new Uri("http://local.mysite.com/wsDirName"), "Digest", userCredential);
    maintain.Credentials = credentials;
    maintain.PreAuthenticate = true;
    Sorry!
    Steve Commisso
    MCSD.NET, MCPD: EAD
  • Re: "401 Not Authorized" error when authenticating a web service

    01-31-2007, 3:12 AM
    Afro Blanca:

    Heh, no luck finding a solution yet.  However, I did upgrade Machine A from Windows Server 2000 to Windows Server 2003.  The result is that, like Machine B, it can no longer call the webservice on itself.  So now we know that :

    1) This is a problem with Win2K3 in general, and not just Machine B's specific setup.

    2) This is a problem that wasn't present in Win2K.

    My guess is that some part of the Windows security model changed between 2K and 2K3.

    I kinda gave up on this for a while, but would really like to find a solution.  I was figuring I'd re-post the question to a few forums some time next week.
     

    I used to have such a problem.

    WebService works fine on XP but result in 401 Not Authorized error on 2k3(setup by others).

    I solved the problem by adding user accont

    LOCAL SERVICE

    NETWORK SERVICE

    read and write permissions on application fold.

    Hope this helps.

    NOTE:If you find my response contains a reference to a third party World Wide Web site, I am providing this information as a convenience to you.Microsoft does not control these sites and has not tested any software or information found on these sites; therefore,Microsoft cannot make any representations regarding the quality,safety, or suitability of any software or information found there.
    __________________________________________________

    Sincerely,
    Young Fang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: "401 Not Authorized" error when authenticating a web service

    02-16-2007, 2:14 PM
    • Loading...
    • feio
    • Joined on 09-10-2006, 11:33 AM
    • Portugal
    • Posts 5

    Hello!

     I also have the same problem.

    I can't seem to get my ASP.Net Web App to access my Web Service when they are both hosted on the same Windows Server 2003.

    I've already tried to switch the account where my app pool works to Local System, but it still doesn't work.

     I've tried giving the permissions you mentioned to my Web Service folder, but it still doesn't work.

    Any ideas?

    Thanks,

    Pedro Feio

  • Re: "401 Not Authorized" error when authenticating a web service

    06-25-2007, 6:07 PM
    • Loading...
    • Afro Blanca
    • Joined on 09-14-2004, 4:01 PM
    • NYC, baby!
    • Posts 100
    Sorry it took me so long to get back to you.  As it turns out, digest authentication works!  Any idea why this would work, but integrated Windows authentication would fail?
  • Re: "401 Not Authorized" error when authenticating a web service

    08-16-2007, 5:12 PM
    • Loading...
    • BigBlueEye
    • Joined on 05-17-2007, 10:14 AM
    • Posts 54

    Hi,

    I tried as you suggested and still get the 401 error. My code is below. Any ideas?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim serviceproxy As New WSBus.ServicesBusWebService()

            Dim userCredential As NetworkCredential = New NetworkCredential("username", "password", "domain")
            Dim credentials As CredentialCache = New CredentialCache()
            credentials.Add(New Uri(serviceproxy.Url), "Digest", userCredential)
            serviceproxy.Credentials = credentials
            serviceproxy.PreAuthenticate = True

            Dim response = serviceproxy.HelloWorld() 'BREAKS HERE
            lblMessage.Text = response

    End Sub

Page 1 of 1 (10 items)
Microsoft Communities
Page view counter