WCF开发框架-客户端采用Certificate认证模式调用基于HTTPS协议的WCF接口
作者:作者不详  发布日期:2021/08/13 18:33:35
  WCF开发框架-客户端采用Certificate认证模式调用基于HTTPS协议的WCF接口

WCF开发框架-客户端采用Certificate证书认证模式调用基于HTTPS协议的WCF接口


本文介绍


在Transport安全模式下,客户端凭据类型(clientCredentialType)支持四种常用类型:None、Basic、Windows、Certificate,默认情况下采用None凭据类型。CSFrameworkV5.1旗舰版提供四种常用凭据类型的例子,本小节主要探讨Certificate凭据。



一、重要参数及设置:


绑定方式:wsHttpBinding
安全模式:Transport
客户端凭据类型(clientCredentialType):Certificate
WCF服务协议:https
IIS的SSL设置:客户证书=接受
web.config配置:behavior行为配置,SSL证书参数
app.config配置:本文3.2章节
客户端部署:必须将pfx证书文件部署到终端用户的电脑


二、服务端web.config配置:



XML Code:

<wsHttpBinding>
<binding name="WSHttpBindings" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
<security mode="Transport">
<transport clientCredentialType="Certificate" proxyCredentialType="None"/>
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>

//来源:C/S框架网 | www.csframework.com | QQ:23404761




三、 客户端app.config配置



3.1 【透明代理工厂】方式创建WCF服务实例依赖的Binding配置:


XML Code:

<binding name="WSHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout
="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false"
transactionFlow
="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize
="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding
="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies
="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength
="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled
="false" />

<security mode="Transport">

<transport clientCredentialType="Certificate" proxyCredentialType="None" />

<message clientCredentialType="None" negotiateServiceCredential="false"
establishSecurityContext
="false" />
</security>
</binding>

//来源:C/S框架网 | www.csframework.com | QQ:23404761




3.2 【添加服务引用】方式创建WCF服务实例依赖的Binding配置:


binding配置:


XML Code:

<binding name="WSHttpBinding_ICommonService2">
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>

//来源:C/S框架网 | www.csframework.com | QQ:23404761


endpoint配置:


XML Code:

<!--endpoint配置。若是certificate证书认证模式,必须配置behaviorConfiguration信息-->
<endpoint address="https://cs5.manonwo.com/CommonService.svc"
binding
="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonService2"
contract
="ServiceReference1.ICommonService" name="WSHttpBinding_ICommonService2"
behaviorConfiguration
="SSL_ClientBehavior" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>

//来源:C/S框架网 | www.csframework.com | QQ:23404761


behaviorConfiguration配置:


XML Code:


<!--endpoint的行为配置:behaviorConfiguration-->
<!--用于配置客户端SSL证书信息-->
<behaviors>
<endpointBehaviors>
<behavior name="SSL_ClientBehavior">
<clientCredentials>
<clientCertificate
findValue="cs5.manonwo.com"
storeLocation
="LocalMachine"
x509FindType
="FindBySubjectName"
storeName
="My"/>
<serviceCertificate>
<authentication certificateValidationMode="None"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>


//来源:C/S框架网 | www.csframework.com | QQ:23404761


findValue=SSL证书的域名,如:cs5.manonwo.com, 或 *.manonwo.com
storeLocation=LocalMachine,SSL证书部署到【本地计算机】。
storeLocation=CurrentUser,SSL证书部署到【当前用户】。




****************************************

注意:

1. 每个WCF服务必须配置binding以及endpoint,本文仅配置ICommonService服务

2. 每个WCF服务的行为配置为:behaviorConfiguration="SSL_ClientBehavior"

****************************************


请仔细核对配置文件的【安全模式】及【凭据类型】:


安全模式(支持HTTPS协议):


<security mode="Transport">


客户端凭据类型(认证类型):

<transport clientCredentialType="Certificate" proxyCredentialType="None"/>





四、客户端测试


4.1 测试方式1 - 【添加服务引用】生成WCF客户端代理类:


在VS解决方案,添加服务引用:输入URI地址,点【转到】按钮:


贴图图片-WCF-SSL-证书认证3


你会发现查找WCF服务发生错误,点击【详细信息】:


贴图图片-WCF-SSL-证书认证4

下载“https://cs5.manonwo.com/CommonService.svc/$metadata”时出错。
请求失败,HTTP 状态为 403: Forbidden。
元数据包含无法解析的引用:“https://cs5.manonwo.com/CommonService.svc”。
使用客户端身份验证方案“Anonymous”禁止 HTTP 请求。
远程服务器返回错误: (403) 已禁止。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。


莫急!!!是因为IIS的当前WCF服务【SSL设置】客户证书为【必需】,
所以您不能以Anonymous(匿名)身份访问WCF接口!


解决方案: 

打开IIS服务器,打开当前网站的【SSL设置】,将客户证书改为【忽略】或【接受】

建议改为【接受】

若改为【忽略】,客户端能正常添加服务引用,但是以Certificate凭据调用WCF接口会报错,到时又要改回来。


贴图图片-WCF-SSL-证书认证5



重启网站服务,然后在VS内再次点【转到】按钮,成功找到1个服务。


贴图图片-WCF-SSL-证书认证7



点【确定】按钮,添加成功后,VS解决方案内自动生成ServiceReference1的服务引用:



贴图图片-WCF_BASIC身份验证_添加服务引用2



如上操作添加WCF服务引用后,修改App.Config配置文件:参考本文3.2章节。




C#代码:调用基于Certificate认证HTTPS协议的WCF服务:



C# Code:


private void button9_Click(object sender, EventArgs e)
{
  
//登录信息
  
byte[] loginTicket = GetLoginer();
  ServiceReference1.CommonServiceClient svc
= new ServiceReference1.CommonServiceClient();
  
  
//调用WCF接口,获取单据号码
  
var no = svc.GetDataSN(loginTicket, "AA", true);
  ShowResult(no);
  svc.Close();
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761





4.2 测试方式2 - 透明代理工厂动态创建基于Certificate认证HTTPS协议的WCF服务



使用透明代理工厂比较简单,C#代码:


C# Code:

private void button10_Click(object sender, EventArgs e)
{
  
//登录信息
  
byte[] loginTicket = GetLoginer();
  
  
//透明代理工厂动态创建WCF接口
  
ICommonService svc = WCFFactory.CreateCertificate<ICommonService>(txtUrl.Text);
  
  
//调用WCF接口,获取单据号码
  
var no = svc.GetDataSN(loginTicket, "AA", true);
  ShowResult(no);
  
  
//关闭WCF
  
(svc as ICommunicationObject).Close();
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761





透明代理工厂动态创建基于Certificate认证的WCF服务:



C# Code:


/// <summary>
/// 动态创建WCF接口透明代理 - Certificate证书认证
/// </summary>
/// <typeparam name="T">WCF接口,如:ICommonService</typeparam>
/// <param name="uri">连接地址</param>
/// <returns></returns>
public static T CreateCertificate<T>(string uri = "") where T : class
{
  
//获取协议配置并创建实例,必须是主程序的App.config配置文件
  
var myBinding = new WSHttpBinding("WSHttpBinding");
  
var myEndpoint = new EndpointAddress(new Uri(uri));
  
var myChannelFactory = new ChannelFactory<T>(myBinding, myEndpoint);
  
  
//ssl证书文件及密码
  
var pfxPath = @"C:\Users\Administrator\Downloads\5914048_cs5.manonwo.com.pfx";
  
var pfxPwd = "C26xjf34";
  
  
//设置客户端证书文件
  
X509Certificate2 clientCer = new X509Certificate2(pfxPath, pfxPwd, X509KeyStorageFlags.MachineKeySet);
  myChannelFactory.Credentials.ClientCertificate.Certificate
= clientCer;
  
  
//创建WCF通道
  
T instance = myChannelFactory.CreateChannel();
  
return instance;
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761






五、测试结果:



贴图图片-WCF-SSL-证书认证8




特别提示!!!

采用Certificate凭据认证方式,必须将pfx证书文件部署到客户的电脑!!!
因此,建议使用Basic或Windows简易认证方式!



C/S框架网|原创精神.创造价值.打造精品



C/S架构WCF快速开发平台-旗舰版V5.1 (Ultimate Edition 2021)

适用开发 适用开发:企业级ERP、MES、MRP、HIS、WMS、TMS、CRM、MIS、POS等数据管理系统
运行平台 运行平台:Windows (Winform) + .NET Framework 4.5
开发工具 开发工具:Visual Studio 2017+,C#语言
数据库 多数据库:MsSQL 2008R2 / MySql5.7.34 / Oracle 11g


C/S架构软件快速开发平台旗舰版v5.1|C#.NET开发平台|Winform开发框架|C/S框架网

 产品介绍

    C/S架构软件快速开发平台助力开发团队快速搭建自己的软件项目,旗舰版提供强大的底层开发架构及快速开发工具-Winform三层架构代码生成器v5.1,旗舰版集成大量应用于大型系统的通用功能模块、数据界面及通用权限管理系统,提供丰富的实例开发模板、开发文档、线上技术指导服务,助力您快速搭建软件项目。

   C/S架构开发框架系列产品已成功应用500多家企业、4000多位软件用户,其中包括国内知名软件公司、国有企业、研发机构及上市公司(优秀企业选择了我们的产品-成功案例)。经过十年迭代升级,最新旗舰版V5.1,基础架构更成熟、应用更广泛、性能更稳定、开发效率更高!


 产品详情

http://www.csframework.com/cs-framework-5.1.htm




关联文章:


WCF开发框架-客户端采用BASIC身份认证调用HTTPS协议WCF接口

http://www.csframework.com/archive/1/arc-1-20210813-3742.htm



WCF开发框架-客户端采用Windows身份认证调用HTTPS协议WCF接口

http://www.csframework.com/archive/1/arc-1-20210813-3743.htm



WCF开发框架默认连接:HTTP协议+Message安全模式+Windows客户端认证

http://www.csframework.com/archive/1/arc-1-20210816-3750.htm


WCF顶级安全:HTTPS协议SSL证书+Windows认证+TransportWithMessageCredential安全模式

http://www.csframework.com/archive/1/arc-1-20210816-3749.htm




C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇