【问题标题】:Web Service 401: Unauthorized ErrorWeb 服务 401:未经授权的错误
【发布时间】:2011-12-15 17:55:22
【问题描述】:

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

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

我需要调整任何 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();

【问题讨论】:

  • IdentityService 是 SOAP 样式的 Web 服务(默认情况下类似于 WCF Web 服务)还是旧的 .NET 2.0 ASMX Web 服务?
  • 会不会是“Basic”不是使用的认证系统? Basic 不使用域属性。如果您尝试使用给定的用户/密码连接到浏览器中的 URI,那么它是否也是 401?
  • 您是否使用 Visual Studio 生成的服务代理类?我通常习惯于在 ClientBase 派生的代理类上摆弄ClientCredentials 属性。或许我们需要看看IdentityService的一些代码。
  • @JonHanna - 是的,如果我转到 URL 并插入那些确切的登录名/密码,我就会通过身份验证。
  • 在 fiddler 运行时再试一次。当你被要求输入用户/通行证时会有一个 401,然后是 200 当你被允许进入时。查看标题应该可以清楚地知道正在使用什么身份验证方案(我打赌是 Digest 或 NTLM) .

标签: c# web-services asmx credentials


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2012-06-29
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多