开发者

Connect to a password protected server with WCF over HTTP

Hi,

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

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>开发者_Python百科

The problem is that I get the following exception :

The remote server returned an error: (401) Unauthorized. 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.Net.WebException: The remote server returned an error: (401) Unauthorized.

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.

Pleas advice

Edit1:

This is how the service looks like :

[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: 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.

Connect to a password protected server with WCF over HTTP

Pleas note, this is an example, the adress and "The site says" is something else.

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

Edit3: 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 :

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.

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?

Edit 4

Created a new question : Unable to deserialize XML body with root name 'Binary' and root namespace?

Thanks Ladislav Mrnka for helping!


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

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜