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


本文介绍
在Transport安全模式下,客户端凭据类型(clientCredentialType)支持四种常用类型:None、Basic、Windows、Certificate,默认情况下采用None凭据类型。CSFrameworkV5.1旗舰版提供四种常用凭据类型的例子,本小节主要探讨Windows凭据类型。
在Transport安全模式下,客户端凭据类型(clientCredentialType)支持四种常用类型:None、Basic、Windows、Certificate,默认情况下采用None凭据类型。CSFrameworkV5.1旗舰版提供四种常用凭据类型的例子,本小节主要探讨Windows凭据类型。
一、重要参数:
绑定方式:wsHttpBinding
安全模式:Transport
客户端凭据类型(clientCredentialType):Windows
WCF服务协议:https
二、服务端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="Windows" proxyCredentialType="None"/>
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<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="Windows" proxyCredentialType="None"/>
<message clientCredentialType="None" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
支持HTTPS协议:
<security mode="Transport">
Windows认证方式:
<transport clientCredentialType="Windows" proxyCredentialType="None"/>
三、客户端app.config配置
3.1 【透明代理工厂】方式创建WCF服务实例依赖的Binding配置:
XML Code:
<security mode="Transport">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="None" negotiateServiceCredential="false"
establishSecurityContext="false" />
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<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" />
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="Windows" proxyCredentialType="None" />
<message clientCredentialType="None" negotiateServiceCredential="false"
establishSecurityContext="false" />
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
支持HTTPS协议:
<security mode="Transport">
Windows认证方式:
<transport clientCredentialType="Windows" proxyCredentialType="None"/>
3.2 【添加服务引用】方式创建WCF服务实例依赖的Binding配置:
binding配置:
XML Code:
<binding name="WSHttpBinding_ICommonService2">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
****************************************
注意: 每个WCF服务必须配置binding以及endpoint,本文仅配置ICommonService服务
****************************************
四、客户端测试
4.1 测试方式1 - 【添加服务引用】生成WCF客户端代理类:
VS解决方案,添加服务引用:输入URI地址,点【转到】按钮:

WCF服务端配置了HTTPS协议以及Windows认证方式,因此客户端添加服务引用时弹出下面的对话框,
请点【否】按钮,最后点【确定】按钮完成添加服务引用。

添加成功后,VS解决方案内自动生成ServiceReference1的服务引用

调用WCF接口C#代码:
C# Code:
private void button7_Click(object sender, EventArgs e)
{
//登录信息
byte[] loginTicket = GetLoginer();
ServiceReference1.CommonServiceClient svc = new ServiceReference1.CommonServiceClient();
//Windows身份认证信息
var identity = new System.Net.NetworkCredential("wcfuser", "wcf.user!2021");
svc.ClientCredentials.Windows.ClientCredential = identity;
//调用WCF接口,获取单据号码
var no = svc.GetDataSN(loginTicket, "AA", true);
ShowResult(no);
svc.Close();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
private void button7_Click(object sender, EventArgs e)
{
//登录信息
byte[] loginTicket = GetLoginer();
ServiceReference1.CommonServiceClient svc = new ServiceReference1.CommonServiceClient();
//Windows身份认证信息
var identity = new System.Net.NetworkCredential("wcfuser", "wcf.user!2021");
svc.ClientCredentials.Windows.ClientCredential = identity;
//调用WCF接口,获取单据号码
var no = svc.GetDataSN(loginTicket, "AA", true);
ShowResult(no);
svc.Close();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
4.2 测试方式2 - 透明代理工厂动态创建基于Windows认证HTTPS协议的WCF服务
C# Code:
private void button8_Click(object sender, EventArgs e)
{
//登录信息
byte[] loginTicket = GetLoginer();
//透明代理工厂动态创建WCF接口
ICommonService svc = WCFFactory.CreateWindows<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
{
//登录信息
byte[] loginTicket = GetLoginer();
//透明代理工厂动态创建WCF接口
ICommonService svc = WCFFactory.CreateWindows<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
透明代理工厂动态创建基于Windows身份认证的WCF服务
C# Code:
/// <summary>
/// 动态创建WCF接口透明代理 - Windows身份认证
/// </summary>
/// <typeparam name="T">WCF接口,如:ICommonService</typeparam>
/// <param name="uri">连接地址</param>
/// <returns></returns>
public static T CreateWindows<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);
//重要!!!Windows身份认证-需要的本地Windows认证的用户名及密码
var identity = new System.Net.NetworkCredential("wcfuser", "wcf.user!2021");
//Windows身份认证信息
myChannelFactory.Credentials.Windows.ClientCredential = identity;
//创建WCF通道
T instance = myChannelFactory.CreateChannel();
return instance;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// <summary>
/// 动态创建WCF接口透明代理 - Windows身份认证
/// </summary>
/// <typeparam name="T">WCF接口,如:ICommonService</typeparam>
/// <param name="uri">连接地址</param>
/// <returns></returns>
public static T CreateWindows<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);
//重要!!!Windows身份认证-需要的本地Windows认证的用户名及密码
var identity = new System.Net.NetworkCredential("wcfuser", "wcf.user!2021");
//Windows身份认证信息
myChannelFactory.Credentials.Windows.ClientCredential = identity;
//创建WCF通道
T instance = myChannelFactory.CreateChannel();
return instance;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
五、测试结果:


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





C/S架构软件快速开发平台助力开发团队快速搭建自己的软件项目,旗舰版提供强大的底层开发架构及快速开发工具-Winform三层架构代码生成器v5.1,旗舰版集成大量应用于大型系统的通用功能模块、数据界面及通用权限管理系统,提供丰富的实例开发模板、开发文档、线上技术指导服务,助力您快速搭建软件项目。
C/S架构开发框架系列产品已成功应用500多家企业、4000多位软件用户,其中包括国内知名软件公司、国有企业、研发机构及上市公司(优秀企业选择了我们的产品-成功案例)。经过十年迭代升级,最新旗舰版V5.1,基础架构更成熟、应用更广泛、性能更稳定、开发效率更高!
开发框架配套的核心工具 - Winform三层架构代码生成器能快速生成界面(FORM)、业务层(BLL)、数据层(DAL)、模型(Model)、报表(Report)以及VS工程项目(Project)的源代码,快速提升开发效率,节约开发时间,降低项目成本,根据多个项目统计,开发框架能为您减少60%以上的工作量。

本文关联文章:
WCF开发框架-客户端采用BASIC身份认证调用HTTPS协议WCF接口
http://www.csframework.com/archive/1/arc-1-20210813-3742.htm
WCF开发框架-客户端采用Certificate认证模式调用基于HTTPS协议的WCF接口
http://www.csframework.com/archive/1/arc-1-20210813-3746.htm
WCF开发框架默认连接:HTTP协议+Message安全模式+Windows客户端认证
WCF顶级安全:HTTPS协议SSL证书+Windows认证+TransportWithMessageCredential安全模式

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网