不能调用Web服务使用WCF的基本身份验证

不能调用Web服务使用WCF的基本身份验证

问题描述:

我已经被赋予了Web服务用Java编写的,我不能够做任何改变。它需要基本身份验证的用户身份验证,访问任何的方法。建议的方式来使用这项服务的.NET交互是通过使用Visual Studio 2005与WSE 3.0安装。

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错误,当我试图做的Web服务什么。

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.

这是我的code是这样的:

This is what my code looks like:

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请求是未经授权的客户端身份验证方案'无名氏'。从服务器接收到的认证标头   是基本境界=境界'。

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm=realm'.

这有一个内部异常:

远程服务器返回错误:(401)未经授权

什么可能会造成这个问题将大大AP preciated任何想法。

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的web服务的期望,以及如何发送相关信息到它。

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.

马克·