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

时间:2011-12-15 17:55:22

标签: c# web-services asmx credentials

我在本地运行一个连接到我无法访问的外部服务器的Web服务。

即使此网络服务的管理员确认测试凭据完全正确,我仍然收到“401:Unauthorized”错误。

我是否需要调整任何IIS设置?

以下是错误的屏幕截图。

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 个答案:

答案 0 :(得分:6)

使用基本身份验证删除以下代码,修复了此问题。

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