C# Web服务401:未经授权的错误

C# Web服务401:未经授权的错误,c#,web-services,asmx,credentials,C#,Web Services,Asmx,Credentials,我正在本地运行一个web服务,该服务连接到我无权访问的外部服务器 我一直收到“401:Unauthorized”错误,即使测试凭据已被此web服务的管理员确认为完全正确 我需要调整任何IIS设置吗 下面是错误的屏幕截图 // Webservice IdentityService ws = new IdentityService(); // Test Static Credentials string username = "twf

我正在本地运行一个web服务,该服务连接到我无权访问的外部服务器

我一直收到“401:Unauthorized”错误,即使测试凭据已被此web服务的管理员确认为完全正确

我需要调整任何IIS设置吗

下面是错误的屏幕截图

        // 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();


通过基本身份验证删除以下代码,修复了该问题

CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(ws.Url), "Basic", credentials);
最终代码工作:

    // 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();

IdentityService
是SOAP风格的web服务(默认情况下与WCF web服务类似)还是较旧的.NET 2.0 ASMX web服务?是否使用了“基本”身份验证系统?Basic不使用域属性。如果您尝试使用给定的user/pass连接到浏览器中的URI,是否也会这样做?是否使用Visual Studio生成的代理类进行服务?我通常习惯于摆弄ClientBase派生的代理类上的属性。也许我们需要查看
IdentityService
@JonHanna中的一些代码-是的,如果我转到URL并插入那些准确的登录/传递,我将获得身份验证。请在运行fiddler的情况下重试。当你被要求提供用户/通行证时,会有一个401,当你被允许进入时,会有一个200。查看标题应该可以清楚地知道使用了什么身份验证方案(我打赌是Digest还是NTLM)。