WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”

本文详细介绍如何在WCF服务中使用Transport模式下基于Basic的客户端凭证类型进行身份验证,包括server和client的web.config配置示例,以及C#客户端代码实现。

 

原创地址: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>


 

031530329245683

image

031530372362965

 

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

 

调用成功。

 

031530530172212

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值