Web服务 401:未授权错误。

4

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

尽管此Web服务的管理员已经确认测试凭据完全正确,但我仍然不断收到“401:未经授权”的错误。

我需要调整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
IdentityService 是一个 SOAP 风格的 Web 服务(默认情况下类似于 WCF Web 服务),还是旧的 .NET 2.0 ASMX Web 服务? - Sean Hanley
1
您是否正在使用Visual Studio生成的服务代理类?我通常习惯于在ClientBase派生的代理类上调整ClientCredentials属性。也许我们需要查看一些来自“IdentityService”的代码。 - Sean Hanley
@JonHanna - 是的,如果我访问该URL并输入那些确切的登录名/密码,我会得到认证。 - the sandman
谢谢大家,移除基本身份验证就解决了问题。虽然我现在遇到了新的错误,但我会尝试解决它。@SeanHanley - 如果你能把这个作为答案,我会标记它为正确的。谢谢。 - the sandman
新的错误,我将进行故障排除:NACK:在FNF.Titlewave.IntegrationServices.Services.Identity.IdentityService.AuthenticateUser()中处理请求时出错。异常-->System.NullReferenceException:未将对象引用设置为对象的实例。 在FNF.Titlewave.IntegrationServices.Core.Controllers.Identity.AuthenticateUserController.AuthenticateUserHelp(AuthenticateUserRequest request)处 在FNF.Titlewave.IntegrationServices.Services.Identity.IdentityService.AuthenticateUser(AuthenticateUserRequest request) - the sandman
显示剩余3条评论
1个回答

7

移除使用基本身份验证的以下代码,解决了问题。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接