使用WCF over HTTP连接到受密码保护的服务器 [英] Connect to a password protected server with WCF over HTTP

查看:139
本文介绍了使用WCF over HTTP连接到受密码保护的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用WCF将我的ASP.NET MVC应用程序连接到网络上的服务器。该服务器受用户名和密码保护,但似乎它不是https而是简单的http。

I need to connect my ASP.NET MVC application to a server on the net with WCF. The server is username and password protected but it appears that its not a https but a simple http.

这是我到目前为止的代码:

This is the code I have so far :

public MyObject GetMyData(string id)
{
    ChannelFactory<MyIService> factory;
    ClientCredentials loginCredentials = new ClientCredentials();

    loginCredentials.UserName.UserName = "MyName";
    loginCredentials.UserName.Password = "MyPassword";

    factory = new ChannelFactory<ICFService>("MyData");

    var defaultCredentials = factory.Endpoint.Behaviors.Find<ClientCredentials>();
    factory.Endpoint.Behaviors.Remove(defaultCredentials); //remove default ones
    factory.Endpoint.Behaviors.Add(loginCredentials); //add required ones

    var proxy = factory.CreateChannel();
    var response = proxy.GetMyData(id);
    ((IDisposable)proxy).Dispose();

    return response as MyObject ;
}

这是放在我的web.config中

And this is placed in my web.config

  <system.serviceModel>
    <client>
      <endpoint address="http://MyUrl/"
                binding="webHttpBinding"
                behaviorConfiguration="MyData"
                contract="MyNameSpace.MyIService"
                name="MyData"/>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyData">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>

问题在于我得到以下异常:

The problem is that I get the following exception :

远程服务器返回错误:(401)未经授权。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Net.WebException:远程服务器返回错误:(401)未经授权。

我知道服务器那个我试图与之通信只会在我的应用程序被放置在特定的IP nr上时进行通信,但这已经完成并且应该有效。

I know that the server that I am trying to communicate with will only communicate if my application is placed on a specific IP nr but this is already done and should work.

请求建议

编辑1:

这就是服务的样子:

[ServiceContract]
[XmlSerializerFormat]
public interface ICFService
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, 
        ResponseFormat = WebMessageFormat.Xml, 
        UriTemplate = "get?UID=12345.eu_vddsall_xml&MyId={MyId}&LANG=en")]
    CFExtendedDescription GetMyData(string MyId);

}

Edit2:
这是在Firefox中浏览url时对话框的样子。我不知道如何检查Proxy.GetMyData将使用的实际网址。

This is how the dialog looks like when browsing the url in firefox. Im not sure how to check the acctual url that the Proxy.GetMyData will use.


请注意,这是一个例子,地址和网站说是别的。

我要使用的网址如下所示:

The Url that I am supose to use looks like this :

http://bat.myserver.com/ag/get?UID=12345.eu_vddsall_xml&MyId=WRH421&LANG=sv

编辑3:
通过向web.config添加愚蠢,我们向前迈了一步(感谢Ladislav Mrnka):

By adding the foolowing to the web.config we got a step forward (thanks Ladislav Mrnka) :

<bindings>
  <webHttpBinding>
    <binding name="secured">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>

现在我们得到以下异常:

Now we get the following exception instead :

无法使用根名称'Binary'和根命名空间''反序列化XML主体(用于操作'GetMyData'和契约('ICFService',' http://tempuri.org/ '))使用XmlSerializer。确保将与XML对应的类型添加到服务的已知类型集合中。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Runtime.Serialization.SerializationException:无法反序列化具有根名称'Binary'和根命名空间''的XML主体(用于操作'GetMyData'和契约('ICFService',' http://tempuri.org/ '))使用XmlSerializer。确保将与XML对应的类型添加到服务的已知类型集合中。

Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

首先我认为远程服务器maby返回了一个xml文档这表明我在错误的IP上,所以我将网站上传到正确的服务器(使用正确的IP),但我在那里得到了同样的例外?

First I thought that the remote server maby returned a xml document that shows that I am on the wrong IP so I uploaded the site to the correct server(with the correct ip) but I get the same exception there?

编辑4

创建了一个新问题:无法使用根名称'Binary'和根命名空间反序列化XML主体?

感谢Ladislav Mrnka提供的帮助!

Thanks Ladislav Mrnka for helping!

推荐答案

如果您不知道怎么做,您应该问服务提供商如何获得服务?找到它。

You should ask service provider how is service secured if you don't know how to find it.

您的网站似乎正在使用基本的HTTP身份验证。对于此类身份验证,您需要创建自定义 webHttpBinding 配置:

It looks like your site is using basic HTTP authentication. For such authentication you need to create custom webHttpBinding configuration:

<system.serviceModel>
  <client>
    <endpoint address="http://MyUrl/"
              binding="webHttpBinding"
              bindingConfiguration="secured"
              behaviorConfiguration="MyData"
              contract="MyNameSpace.MyIService"
              name="MyData"/>
  </client>
  <bindings>
    <webHttpBinding>
      <binding name="secured">
        <security mode="TransportCredentialOnly">
          <transport clientCredentialType="Basic" />
        </security>
      </binding>
    </webHttpBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="MyData">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

这篇关于使用WCF over HTTP连接到受密码保护的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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