首页 > 代码库 > WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”
WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”
原创地址:http://www.cnblogs.com/jfzhu/p/4071342.html
转载请注明出处
如何在WCF中使用Transport Security Mode,以及如何创建证书,请参见《WCF basicHttpBinding之Transport Security Mode, clientCredentialType="None"》,本文介绍如何使用Basic clientCredentialType。
server web.config
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="wsHttpBindingConfig"> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </wsHttpBinding> </bindings> <services> <service name="WCFDemo.DemoService" behaviorConfiguration="CustomBehavior"> <endpoint address="DemoService" binding="wsHttpBinding" contract="WCFDemo.IDemoService" bindingConfiguration="wsHttpBindingConfig" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CustomBehavior"> <serviceMetadata httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> </configuration>
client app.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IDemoService"> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="https://win-ounm08eqe64.henry.huang/DemoService.svc/DemoService" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDemoService" contract="DemoServiceReference.IDemoService" name="WSHttpBinding_IDemoService" /> </client> </system.serviceModel> </configuration>
public partial class Form1 : Form { DemoServiceReference.DemoServiceClient demoServiceClient; public Form1() { InitializeComponent(); demoServiceClient = new DemoServiceReference.DemoServiceClient(); demoServiceClient.ClientCredentials.UserName.UserName = "alex"; demoServiceClient.ClientCredentials.UserName.Password = "123456"; } private void buttonCalculate_Click(object sender, EventArgs e) { try { textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString(); } catch (FaultException<DemoServiceReference.DivideByZeroFault> fault) { MessageBox.Show(fault.Detail.Error + " - " + fault.Detail.Detail); } } }
调用成功。
WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。