WCF:更改 ClientCredentials 会产生 "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed."

标签 wcf wcf-security wcf-rest

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

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

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

我有点失落。

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

最佳答案

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

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

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

关于WCF:更改 ClientCredentials 会产生 "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4759672/

相关文章:

silverlight - RIA 服务与 WCF 服务

c# - WCF Web 服务中的安全上下文 token 无效或过期

WCF、安全和证书

javascript - 无法将 JSON(从 WCFRest 返回)绑定(bind)到 HTML 表

android - 使用对象作为参数从 Java 调用 RESTful WCF 服务方法

c# - 如何使用 basicHttpBinding(SOAP,而非 REST)启用 Wcf 帮助页面

wcf - wcf中的knowntype属性问题

c# - 序列化任何自定义 WCF SecurityToken 的好方法?

ASP.NET Web Api (REST) : Authentication using the users credentials or a token? 保留 "Register new user"资源密码吗?

c# - 如何从 WCF 中的客户端消息检查器获取 header 值