16

我得到了一个用 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。

任何关于可能导致此问题的想法将不胜感激。

4

3 回答 3

29

第一个问题:这是您尝试调用的 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 Web 服务期望什么以及如何将相关信息发送给它的信息。

马克

于 2009-09-02T05:05:11.950 回答
8

首先将以下内容放入您的 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();
                }
            }
于 2013-10-16T15:22:07.723 回答
3

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

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

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

瞧——它奏效了。

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

于 2010-05-06T21:06:17.740 回答