WCF:更改 ClientCredentials 会产生“此工厂启用手动寻址,因此发送的所有消息都必须预先寻址。”

发布于 2024-10-14 00:08:20 字数 1079 浏览 6 评论 0原文

任何人都可以帮忙,我正在尝试通过通道工厂调用休息服务,但发送我的凭据...休息服务使用 Windows 身份验证。

但通过以下代码,我得到“该工厂启用了手动寻址,因此发送的所有消息都必须预先寻址”。使用 GetMessage 时出错

我知道我的服务就像删除 Windows 身份验证一样工作!但是,在打开 Windows 身份验证并且不更改 clientCredentials 的情况下,我收到了 BAD REQUEST,我认为这是正常的...所以我需要发送我的客户端凭据,

我有点迷失了。

   ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000");


  var defaultCredentials = cf.Endpoint.Behaviors.Find<ClientCredentials>();
  cf.Endpoint.Behaviors.Remove(defaultCredentials); 


  // step two - instantiate your credentials
  ClientCredentials loginCredentials = new ClientCredentials();
  loginCredentials.UserName.UserName = "Test";
  loginCredentials.UserName.Password = "test";


  // step three - set that as new endpoint behavior on factory
  cf.Endpoint.Behaviors.Add(loginCredentials); //add required ones


        IService channel = cf.CreateChannel();

        Console.WriteLine(channel.GetMessage("Dhananjay Get"));

        Console.WriteLine(channel.PostMessage("Dhananjay Post"));

can anyone help, i am trying to call a rest service via the channel factory but sending along my credentials... The rest service uses Windows authentication.

But with the following code i get "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed." error when using GetMessage

I know my service works as if i remove Windows authentication it works! BUt with windows authentication on and not changing clientCredentials i get BAD REQUEST whioch i think is normal... so i need to send along my client credentials

I am a little lost.

   ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000");


  var defaultCredentials = cf.Endpoint.Behaviors.Find<ClientCredentials>();
  cf.Endpoint.Behaviors.Remove(defaultCredentials); 


  // step two - instantiate your credentials
  ClientCredentials loginCredentials = new ClientCredentials();
  loginCredentials.UserName.UserName = "Test";
  loginCredentials.UserName.Password = "test";


  // step three - set that as new endpoint behavior on factory
  cf.Endpoint.Behaviors.Add(loginCredentials); //add required ones


        IService channel = cf.CreateChannel();

        Console.WriteLine(channel.GetMessage("Dhananjay Get"));

        Console.WriteLine(channel.PostMessage("Dhananjay Post"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场春暖 2024-10-21 00:08:20

您需要添加 webHttp 行为,并将该行为连接到您的端点。最终结果将如下所示:

<system.serviceModel>
  <services>
    <service ...>
       <endpoint behaviorConfiguration="webHttpBehavior" ...>
       </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp/>
    </behavior>
    </endpointBehaviors>
  </behaviors>
  ...
 </system.serviceModel>

如果这没有帮助,请发布您的 web.config。

You need to add a webHttp behavior, and wire that behavior up to your endpoint. The end result will look something like this:

<system.serviceModel>
  <services>
    <service ...>
       <endpoint behaviorConfiguration="webHttpBehavior" ...>
       </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp/>
    </behavior>
    </endpointBehaviors>
  </behaviors>
  ...
 </system.serviceModel>

If this doesn't help, please post your web.config.

李白 2024-10-21 00:08:20

尽管这个问题有一个公认的答案,但我想我应该添加一些信息。

由于 WebServiceFactory 类的 GetQueryStringConverter 中存在错误(请参阅Microsoft Connect 报告),如果您通过,则无法使用 通过参数的对象数组。相反,请将 元素添加到绑定中。

Client App.Config 或 Web.Config

<behaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_Behaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

Service Web.Config

<behaviors>
  <serviceBehaviors>
    ...
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_EndPointBehaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

至少,我必须向两个客户端添加 和服务器。

Although this issue has an accepted answer, I thought I'd add a little information.

Because of the bug in GetQueryStringConverter of the WebServiceFactory class (see Microsoft Connect report), you cannot use <WebHttp/> if you are passing array's of objects through a parameter. Instead, add the <enableWebScript/> element to the binding.

Client App.Config or Web.Config

<behaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_Behaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

Service Web.Config

<behaviors>
  <serviceBehaviors>
    ...
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttp_EndPointBehaviour">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

At least, I have had to add <enableWebScript\> to both client and server.

情仇皆在手 2024-10-21 00:08:20

对我来说,我需要将 添加到客户端的行为中。

For me I needed to add <webHttp/> to Behaviours on client side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文