【问题标题】:Can not call web service with basic authentication using WCF无法使用 WCF 调用具有基本身份验证的 Web 服务
【发布时间】:2009-09-01 23:32:26
【问题描述】:

我收到了一个用 Java 编写的 Web 服务,但我无法对其进行任何更改。它要求用户使用基本身份验证进行身份验证才能访问任何方法。在 .NET 中与此服务交互的建议方法是使用安装了 WSE 3.0 的 Visual Studio 2005。

这是一个问题,因为该项目已经在使用 Visual Studio 2008(面向 .NET 2.0)。我可以在 VS2005 中做到这一点,但是我不想将项目绑定到 VS2005,或者通过在 VS2005 中创建程序集并将其包含在 VS2008 解决方案中来实现(无论如何,这基本上将项目与 2005 联系起来,以便将来对程序集进行任何更改)。我认为这些选项中的任何一个都会迫使新开发人员安装 WSE 3.0 并阻止项目在未来使用 2008 和 .NET 3.5 中的功能……也就是说,我真的相信使用 WCF是要走的路。

我一直在研究为此使用 WCF,但是我不确定如何让 WCF 服务了解它需要与每个请求一起发送身份验证标头。当我尝试对 Web 服务执行任何操作时,出现 401 错误。

这是我的代码的样子:

WebHttpBinding webBinding = new WebHttpBinding();
ChannelFactory<MyService> factory = 
     new ChannelFactory<MyService>(webBinding, new EndpointAddress("http://127.0.0.1:80/Service/Service/"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "password";

MyService proxy = factory.CreateChannel();
proxy.postSubmission(_postSubmission);

这将运行并抛出以下异常:

HTTP 请求未通过客户端身份验证方案“匿名”进行授权。从服务器接收到的认证头 是“基本领域=领域”。

这有一个内部异常:

远程服务器返回错误:(401) Unauthorized.

对于可能导致此问题的任何想法,我们将不胜感激。

【问题讨论】:

    标签: wcf web-services .net-3.5 wcf-security


    【解决方案1】:

    第一个问题:这是您尝试调用的基于 SOAP 还是基于 REST 的 Java 服务?

    现在,通过“webHttpBinding”,您正在使用基于 REST 的方法。如果 Java 服务是 SOAP 服务,那么您需要将绑定更改为“basicHttpBinding”。

    如果这是一个基于 SOAP 的服务,你应该试试这个:

    BasicHttpBinding binding = new BasicHttpBinding();
    
    binding.SendTimeout = TimeSpan.FromSeconds(25);
    
    binding.Security.Mode = BasicHttpSecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = 
                                  HttpClientCredentialType.Basic;
    
    EndpointAddress address = new EndpointAddress(your-url-here);
    
    ChannelFactory<MyService> factory = 
                 new ChannelFactory<MyService>(binding, address);
    
    MyService proxy = factory.CreateChannel();
    
    proxy.ClientCredentials.UserName.UserName = "username";
    proxy.ClientCredentials.UserName.Password = "password";
    

    我已经将它与各种 Web 服务一起使用,并且它在大多数情况下都能正常工作。

    如果这不起作用,您将不得不了解更多关于 Java 网络服务期望什么以及如何将相关信息发送给它的信息。

    马克

    【讨论】:

    • Marc,如果它是基于 REST 的 Java 服务,你能告诉我如何编码和配置吗?
    • 在我输入代理后,属性 ClientCredentials 根本没有出现。
    • @rajibdotnet:好吧,问题中的代码基本上就是您需要调用基于 REST 的服务的内容......如果这没有帮助:请提出您自己的问题 所以人们可以回答...
    • @marc_s 在 java 中相当于什么...我还得到了一个使用这种身份验证的示例 Web 服务客户端...我需要将其转换为爪哇
    • @Sergiu:抱歉,我对 Java 的了解不够,无法为您转换它
    【解决方案2】:

    首先将以下内容放入您的 app.config 或 web.config。 (在环境中移动时无需更改它):

    <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IConfigService">
                        <security mode="TransportCredentialOnly">
                            <transport clientCredentialType="Basic"/>
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:55283/ConfigService.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IConfigService"
                    contract="IConfigService" name="BasicHttpBinding_IService" />
            </client>
        </system.serviceModel>
    

    相应地将合同属性更改为 Namespace.Interface 名称。 注意安全模式 = TransportCredentialOnly

    现在要以编程方式更改端点并传递凭据,请使用以下代码:

                var myBinding = new BasicHttpBinding("BasicHttpBinding_IConfigService");
                var myEndpoint = new EndpointAddress("http://yourbaseurl/configservice.svc");
                var myChannelFactory = new ChannelFactory<IConfigService>(myBinding, myEndpoint);
    
                var credentialBehaviour = myChannelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
                credentialBehaviour.UserName.UserName = @"username";
                credentialBehaviour.UserName.Password = @"password";
    
                IConfigService client = null;
    
                try
                {
                    client = myChannelFactory.CreateChannel();
                    var brands = client.YourServiceFunctionName();
                    ((ICommunicationObject)client).Close();
                }
                catch (Exception ex)
                {
                    if (client != null)
                    {
                        ((ICommunicationObject)client).Abort();
                    }
                }
    

    【讨论】:

    • 我不明白 credentialBehavior 是如何应用于频道的?看起来您只是 Find 客户端凭据并将其应用于新的 Var credentialBehavior。然后您设置该变量的用户名和密码...如果我没记错的话,这与绑定或没有任何关系
    【解决方案3】:

    我也会根据我刚刚遇到的类似问题对此进行补充。我用 VS 自动生成了配置/代理——但它创建的配置实际上并没有工作。

    虽然它正确设置了安全模式="Transport",但没有设置clientCredentialType="Basic"。我添加了我的配置,但它仍然无法正常工作。然后我实际上删除了该工具创建的消息安全性,因为我正在联系的服务仅是 SSL + Basic:

    <message clientCredentialType="UserName" algorithmSuite="Default" />
    

    瞧——它奏效了。

    考虑到元素没有指定消息级别的安全性,我不确定为什么这会产生影响......但确实如此。

    【讨论】:

    • 我得到“无法识别的属性‘algorithmSuite’,它对我不起作用。Windows 10 客户端上的 Visual Studio 2017,服务器端未知。
    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多