4
votes

I am running a web service locally that connects to an outside server that i do not have access to.

I keep receiving the "401: Unauthorized" error even though the test credentials are confirmed exactly correct by the admins of this web service.

Do i need to adjust any IIS settings?

Here is a screenshot of the error.

enter image description here

        // Webservice
        IdentityService ws = new IdentityService();

        // Test Static Credentials
        string username = "twfnf";
        string password = "testme99";
        string domain = "testapps1";

        NetworkCredential credentials = new NetworkCredential(username, password, domain);

        CredentialCache credCache = new CredentialCache();
        credCache.Add(new Uri(ws.Url), "Basic", credentials);

        ws.Credentials = credCache;
        ws.PreAuthenticate = true;
        ws.AuthenticateUser();
1
Is IdentityService a SOAP style web service (like WCF web services, by default) or the older .NET 2.0 ASMX web service? - Sean Hanley
Could it be that "Basic" is not the auth system used? Basic doesn't use the domain property. If you try to connect to the URI in a browser with the user/pass given, does it 401 then too? - Jon Hanna
Are you using the Visual Studio generated proxy class for the service? I'm typically used to fiddling with the ClientCredentials property on a ClientBase-derived proxy class. Maybe we need to see some code from IdentityService. - Sean Hanley
@JonHanna - Yes, if i go to the URL and plug in those exact Login/Pass, i get authenticated. - the sandman
Try it again with fiddler running. There would have been a 401 when you got asked for the user/pass, and then the 200 when you got let in. Looking at the headers should make it clear what auth scheme was in use (I'm betting either Digest or NTLM). - Jon Hanna

1 Answers

6
votes

Removing the following code with Basic Authentication, fixed the issue.

CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(ws.Url), "Basic", credentials);

Final Code Working:

    // Webservice
    IdentityService ws = new IdentityService();

    // Test Static Credentials
    string username = "twfnf";
    string password = "testme99";
    string domain = "testapps1";

    NetworkCredential credentials = new NetworkCredential(username, password, domain);

    ws.Credentials = credentials;
    ws.PreAuthenticate = true;
    ws.AuthenticateUser();