无法使用 WCF 调用具有基本身份验证的 Web 服务 [英] Can not call web service with basic authentication using WCF

查看:32
本文介绍了无法使用 WCF 调用具有基本身份验证的 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been given a web service written in Java that I'm not able to make any changes to. It requires the user authenticate with basic authentication to access any of the methods. The suggested way to interact with this service in .NET is by using Visual Studio 2005 with WSE 3.0 installed.

这是一个问题,因为该项目已经在使用 Visual Studio 2008(针对 .NET 2.0).我可以在 VS2005 中做到这一点,但是我不想将项目与 VS2005 联系起来,或者通过在 VS2005 中创建一个程序集并将其包含在 VS2008 解决方案中来做到这一点(这基本上将项目与 2005 联系起来,以便将来对程序集进行任何更改).我认为这两个选项中的任何一个都会迫使新开发人员安装 WSE 3.0 并阻止项目在未来使用 2008 和 .NET 3.5 中的功能,从而使事情变得复杂......即,我真的相信使用 WCF是要走的路.

This is an issue, since the project is already using Visual Studio 2008 (targeting .NET 2.0). I could do it in VS2005, however I do not want to tie the project to VS2005 or do it by creating an assembly in VS2005 and including that in the VS2008 solution (which basically ties the project to 2005 anyway for any future changes to the assembly). I think that either of these options would make things complicated for new developers by forcing them to install WSE 3.0 and keep the project from being able to use 2008 and features in .NET 3.5 in the future... ie, I truly believe using WCF is the way to go.

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

I've been looking into using WCF for this, however I'm unsure how to get the WCF service to understand that it needs to send the authentication headers along with each request. I'm getting 401 errors when I attempt to do anything with the web service.

这是我的代码的样子:

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

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

This will run and throw the following exception:

HTTP 请求未经授权,客户端身份验证方案为匿名".从服务器收到的认证头是基本领域=领域".

这有一个内部例外:

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

如果您对可能导致此问题的原因有任何想法,我们将不胜感激.

Any thoughts about what might be causing this issue would be greatly appreciated.

推荐答案

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

First question: is this a SOAP or a REST based Java service you're trying to call?

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

Right now, with the "webHttpBinding", you're using a REST-based approach. If the Java service is a SOAP service, then you'd need to change your binding to be "basicHttpBinding" instead.

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

IF it's a SOAP based service, you should try this:

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";

我已经将它与各种网络服务一起使用,并且在大多数情况下都有效.

I've used this with various web services and it works - most of the time.

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

If that doesn't work, you'll have to find out more about what that Java webservice expects and how to send that relevant info to it.

马克

这篇关于无法使用 WCF 调用具有基本身份验证的 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆