在C#WCF客户端编程方式的创建头(WSSE)第 [英] Creating Headers (wsse) Section of WCF Client Programatically in C#

查看:187
本文介绍了在C#WCF客户端编程方式的创建头(WSSE)第的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使在C#中的app.config的服务设置的以下部分编程方式:

 <客户端> 
<端点地址=htt​​ps://开头someServiceUrl
结合=basicHttpBinding的bindingConfiguration =联系
合同=ServiceReference.PostingWebServiceNAME =PostingWebServicePort>
<集管和GT;
< WSSE:安全的xmlns:WSSE =htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd>
< WSSE:用户名令牌>
< WSSE:用户名>&someusername LT; / WSSE:用户名>
< WSSE:密码类型=HTTP://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'> somepassword< / WSSE:密码>
< / WSSE:用户名令牌>
< / WSSE:安全和GT;
< /头>
< /端点>
< /客户>



我已成功地生成C#绑定区段(以上不含)和端点部分。我是无法创建头部分



这来了是错误:(这是因为我没有头一节时,我产生在C#中的一切)



此服务需要< WSSE:安全> ,这是缺少



头部分是很重要的,因为如果我从配置中排除和使用的配置中也给出了上面的错误运行代码。



我不希望使用的web.config /的app.config。我要运行从C#的每一件事情。 (以上的app.config工作正常,但我想这样做同样的通过C#)



注意:下面的更新是基于以下提供的解决方案请通过评论该解决方案在下面,为了更好地理解



更新1:(编程方式使用basicHttpBinding的第一个)

  basicHttpBinding的约束力=新basicHttpBinding的(); 
binding.Name =联系;
binding.CloseTimeout = TimeSpan.FromMinutes(1);
binding.OpenTimeout = TimeSpan.FromMinutes(1);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.SendTimeout = TimeSpan.FromMinutes(1);
binding.AllowCookies = FALSE;
binding.BypassProxyOnLocal = FALSE;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferSize = 524288;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 524288;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = TRUE;

binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 65536;
binding.ReaderQuotas.MaxArrayLength = 131072;
binding.ReaderQuotas.MaxBytesPerRead = 32768;
binding.ReaderQuotas.MaxNameTableCharCount = 131072;

binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm =;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;

CustomBinding customBinding =新CustomBinding(绑定);
SecurityBindingElement元= customBinding.Elements.Find< SecurityBindingElement>();
//删除安全时间戳,因为它不使用你原来的绑定
//element.IncludeTimestamp = FALSE; (元素为空在我的情况)

的EndpointAddress端点=新的EndpointAddress(https://开头myserviceaddress);

PostingWebServiceClient客户端=新PostingWebServiceClient(customBinding,终点);

client.ClientCredentials.UserName.UserName =名为myusername;
client.ClientCredentials.UserName.Password =输入mypassword;

client.getActiveChannels(新getActiveChannels());






使用自定义Bindgin直接:

  SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); 
securityElement.IncludeTimestamp = FALSE;
TextMessageEncodingBindingElement encodingElement =新TextMessageEncodingBindingElement(MessageVersion.Soap11,Encoding.UTF8);
HttpsTransportBindingElement transportElement =新HttpsTransportBindingElement();

CustomBinding customBinding =新CustomBinding(securityElement,encodingElement,transportElement);


的EndpointAddress端点=新的EndpointAddress(https://开头myserviceaddress);

PostingWebServiceClient客户端=新PostingWebServiceClient(customBinding,终点);

client.ClientCredentials.UserName.UserName =名为myusername;
client.ClientCredentials.UserName.Password =输入mypassword;

client.getActiveChannels(新getActiveChannels());


解决方案

您不必直接在此配置头这样,因为你的方案应该由 basicHttpBinding的 CustomBinding 直接


$ b $支持b

如果您需要从C#配置它,你必须在代码中创建绑定:

  //助手结合有运输使用用户名令牌
basicHttpBinding的约束力的安全=新basicHttpBinding的(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
// Rest的绑定配置的来这里

//自定义绑定有机会获得基本的约束力
CustomBinding customBinding =新CustomBinding(绑定)的更多配置信息;
SecurityBindingElement元= customBinding.Elements.Find< SecurityBindingElement>();
//删除安全时间戳,因为它不使用你原来的绑定
element.IncludeTimestamp = FALSE;

的EndpointAddress地址=新的EndpointAddress(https://开头...);

ProxyWebServiceClient客户端=新ProxyWebServiceClient(customBinding,地址);
client.ClientCredentials.UserName.UserName =...;
client.ClientCredentials.UserName.Password =...;



其他的解决方案是构建自定义直接结合,而不是与基本的结合开始:

  SecurityBindingElemetn securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); 
securityElement.IncludeTimestamp = FALSE;
TextMessageEncodingBindingElement encodingElement =新TextMessageEncodingBindingElement(MessageVersion.Soap11,Encoding.UTF8);
HttpsTransportBindingElement tranportElement =新HttpsTransportBindingElement();基本绑定

//其他配置分为
//编码和传输元素

CustomBinding customBinding =新CustomBinding(securityElement,encodingElement,transportElement)的性能;

的EndpointAddress地址=新的EndpointAddress(https://开头...);

ProxyWebServiceClient客户端=新ProxyWebServiceClient(customBinding,地址);
client.ClientCredentials.UserName.UserName =...;
client.ClientCredentials.UserName.Password =...;


how do make a the following section of Service Settings of app.config in c# programatically:

    <client>
  <endpoint address="https://someServiceUrl"
      binding="basicHttpBinding" bindingConfiguration="Contact"
      contract="ServiceReference.PostingWebService" name="PostingWebServicePort">
    <headers>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsse:UsernameToken>
          <wsse:Username>someusername</wsse:Username>
          <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>somepassword</wsse:Password>
        </wsse:UsernameToken>
      </wsse:Security>
    </headers>
  </endpoint>
</client>

I have managed to generate binding section (not included above) and endpoint section from c#. I am unable to create the headers section.

The error that comes up is: (this is because i don't have headers section when i generate everything from c#)

This service requires <wsse:Security>, which is missing.

the headers section is important, as if i exclude it from the config and run the code using config it also gives the above error.

I don't want to use web.config/app.config. I have to run every thing from C#. (the above app.config works fine, but i want to do that same through c#)

NOTE: THE UPDATES BELOW ARE BASED ON THE SOLUTION PROVIDED BELOW PLEASE GO THROUGH THE COMMENTS ON THE SOLUTION BELOW, FOR BETTER UNDERSTANDING

UPDATE 1: (programatically using BasicHttpBinding first)

BasicHttpBinding binding = new BasicHttpBinding();
        binding.Name = "Contact";
        binding.CloseTimeout = TimeSpan.FromMinutes(1);
        binding.OpenTimeout = TimeSpan.FromMinutes(1);
        binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
        binding.SendTimeout = TimeSpan.FromMinutes(1);
        binding.AllowCookies = false;
        binding.BypassProxyOnLocal = false;
        binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        binding.MaxBufferSize = 524288;
        binding.MaxBufferPoolSize = 524288;
        binding.MaxReceivedMessageSize = 524288;
        binding.MessageEncoding = WSMessageEncoding.Text;
        binding.TextEncoding = System.Text.Encoding.UTF8;
        binding.TransferMode = TransferMode.Buffered;
        binding.UseDefaultWebProxy = true;

        binding.ReaderQuotas.MaxDepth = 32;
        binding.ReaderQuotas.MaxStringContentLength = 65536;
        binding.ReaderQuotas.MaxArrayLength = 131072;
        binding.ReaderQuotas.MaxBytesPerRead = 32768;
        binding.ReaderQuotas.MaxNameTableCharCount = 131072;

        binding.Security.Mode = BasicHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        binding.Security.Transport.Realm = "";
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
        binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;

        CustomBinding customBinding = new CustomBinding(binding);
        SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
        // Remove security timestamp because it is not used by your original binding
        //element.IncludeTimestamp = false; (element is NULL in my case)

EndpointAddress endpoint = new EndpointAddress("https://myserviceaddress");

        PostingWebServiceClient client = new PostingWebServiceClient(customBinding, endpoint);

        client.ClientCredentials.UserName.UserName = "myusername";
        client.ClientCredentials.UserName.Password = "mypassword";

        client.getActiveChannels(new getActiveChannels());


Using Custom Bindgin Directly:

SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
        securityElement.IncludeTimestamp = false;
        TextMessageEncodingBindingElement encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
        HttpsTransportBindingElement transportElement = new HttpsTransportBindingElement();

        CustomBinding customBinding = new CustomBinding(securityElement, encodingElement, transportElement);


EndpointAddress endpoint = new EndpointAddress("https://myserviceaddress");

        PostingWebServiceClient client = new PostingWebServiceClient(customBinding, endpoint);

        client.ClientCredentials.UserName.UserName = "myusername";
        client.ClientCredentials.UserName.Password = "mypassword";

        client.getActiveChannels(new getActiveChannels());

解决方案

You don't have to configure header directly in this case because your scenario should be supported by BasicHttpBinding or CustomBinding directly.

If you need to configure it from C# you must create binding in code:

// Helper binding to have transport security with user name token
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
// Rest of your binding configuration comes here

// Custom binding to have access to more configuration details of basic binding
CustomBinding customBinding = new CustomBinding(binding);
SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
// Remove security timestamp because it is not used by your original binding
element.IncludeTimestamp = false;

EndpointAddress address = new EndpointAddress("https://...");

ProxyWebServiceClient client = new ProxyWebServiceClient(customBinding, address);
client.ClientCredentials.UserName.UserName = "...";
client.ClientCredentials.UserName.Password = "...";

Other solution is building custom binding directly instead of starting with basic binding:

SecurityBindingElemetn securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.IncludeTimestamp = false; 
TextMessageEncodingBindingElement encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
HttpsTransportBindingElement tranportElement = new HttpsTransportBindingElement();

// Other configurations of basic binding are divided into properties of 
// encoding and transport elements

CustomBinding customBinding = new CustomBinding(securityElement, encodingElement, transportElement);

EndpointAddress address = new EndpointAddress("https://...");

ProxyWebServiceClient client = new ProxyWebServiceClient(customBinding, address);
client.ClientCredentials.UserName.UserName = "...";
client.ClientCredentials.UserName.Password = "...";

这篇关于在C#WCF客户端编程方式的创建头(WSSE)第的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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