如何在代码wcf-wsSecurity中设置用户和密码? [英] How do I set user and password in code wcf-wsSecurity?
本文介绍了如何在代码wcf-wsSecurity中设置用户和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的app.config中有这个端点,但问题是:
如何通过代码从UsernameToken设置用户和密码?
< endpoint 地址 = https: //10.235.135.219/soap/\" binding = basicHttpBinding bindingConfiguration = PlacetoPay_EfectyBinding < span class =code-attribute> 合同 = ServicioPlaceToPay.PlacetoPay_EfectyPort name = PlacetoPay_EfectyPort >
< 标题 >
< wsse:安全 >
< wsse:UsernameToken >
< wsse:用户名 > juan < / wsse:用户名 >
< wsse:密码 > j321 * < / wsse:密码 >
< / wsse:UsernameToken >
< / wsse:Security >
< / headers >
< / endpoint >
我尝试了下一个示例,但它不起作用:
client.ClientCredentials.UserName.UserName = juan;
client.ClientCredentials.UserName.Password = j321 *;
感谢您的回答。
解决方案
它可以在运行时完成,但它确实应该设置在主应用程序之外。你可以这样做:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings [UserName]。Value =myUserName;
config.Save(ConfigurationSaveMode.Modified);
更多详情如下:
ConfigurationManager类(System.Configuration) [ ^ ]
我得到了解决方案:
ServicePointManager.ServerCertificateValidationCallback =
( object s,X509Certificate certificate,X509Chain chain,
SslPolicyErrors sslPolicyErrors)= > true < /跨度>;
// 步骤1:创建绑定
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);
// 第2步:创建endpointAddress
EndpointAddress endpoint = new EndpointAddress( https://10.235.135.219: 8443 /肥皂/ efecty /跨度>);
// 步骤3:创建客户端并关联绑定和端点到客户端
ServicioPlaceToPay.PlacetoPay_EfectyPortClient client = new PlacetoPay_EfectyPortClient(customBinding,endpoint);
// 步骤4:添加客户端凭证
客户端。 ClientCredentials.UserName.UserName = juan;
client.ClientCredentials.UserName.Password = j321 *;
// 第5步:调用方法并检查现在是否正常工作
ServicioPlaceToPay.BillResponse a = client.getBillByReference( 1248);
I have this endpoint in my app.config but the question is:
How can I set the user and password from UsernameToken by code?
<endpoint address="https://10.235.135.219/soap/" binding="basicHttpBinding" bindingConfiguration="PlacetoPay_EfectyBinding" contract="ServicioPlaceToPay.PlacetoPay_EfectyPort"name="PlacetoPay_EfectyPort">
<headers>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>juan</wsse:Username>
<wsse:Password>j321*</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
I tried with the next example but it doesn't work:
client.ClientCredentials.UserName.UserName = "juan";
client.ClientCredentials.UserName.Password = "j321*";
Thanks for your answers.
解决方案
It can be done at runtime but really it should be set outside the main application. You can do it by:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["UserName"].Value = "myUserName";
config.Save(ConfigurationSaveMode.Modified);
More details below:
ConfigurationManager Class (System.Configuration)[^]
I got the solution:
ServicePointManager.ServerCertificateValidationCallback = (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true; //STEP 1: Create Binding 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); //STEP 2: Create endpointAddress EndpointAddress endpoint = new EndpointAddress("https://10.235.135.219:8443/soap/efecty/"); //STEP 3: Create Client and associate the "binding" and the "endpoint" to client ServicioPlaceToPay.PlacetoPay_EfectyPortClient client = new PlacetoPay_EfectyPortClient(customBinding, endpoint); //STEP 4: Add client credentials client.ClientCredentials.UserName.UserName = "juan"; client.ClientCredentials.UserName.Password = "j321*"; //STEP 5: Invoke the method and check if now it's working ServicioPlaceToPay.BillResponse a = client.getBillByReference("1248");
这篇关于如何在代码wcf-wsSecurity中设置用户和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文