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


本文介绍
Windows Communication Foundation (WCF)安全在大多数预定义的绑定上支持三个常见安全模式:
Windows Communication Foundation (WCF)安全在大多数预定义的绑定上支持三个常见安全模式:
Transport:传输模式(支持https协议)。
Message:消息模式(http协议)。
TransportWithMessageCredential:带有消息凭据的传输(支持https协议)。
本主题主要讨论《Message-消息凭据传输》安全模式。
四种常用客户端凭据类型
在Message安全模式下,客户端凭据类型(clientCredentialType)支持五种常用类型:None、Windows、UserName、Certificate、IssuedToken,默认情况下采用None凭据类型。CSFrameworkV5.1旗舰版提供四种常用凭据类型的例子,本小节主要探讨Message安全模式+Windows凭据类型。
一、重要参数:
绑定方式:wsHttpBinding
安全模式:Message
Transport安全模式客户端凭据类型(clientCredentialType):Windows
Message安全模式客户端凭据类型(clientCredentialType):Windows
negotiateServiceCredential:true 重要!!!
WCF服务协议:http
二、服务端web.config配置:
XML Code:
<security mode="Message">
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<!--wsHttpBindings配置-->
<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"/>
<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="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"/>
<message clientCredentialType="Windows" establishSecurityContext="false" negotiateServiceCredential="true"/>
</security>
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
支持HTTPS协议:
<security mode="Message">
Windows认证方式:
<transport clientCredentialType="Windows" proxyCredentialType="None"/>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
establishSecurityContext="false" />
三、客户端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="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<wsHttpBinding>
<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="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
支持HTTPS协议:
<security mode="Message">
Windows认证方式:
<transport clientCredentialType="Windows" proxyCredentialType="None"/>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
establishSecurityContext="false" />
3.2 【添加服务引用】方式创建WCF服务实例依赖的Binding配置:
binding配置:
XML Code:
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" establishSecurityContext="false" negotiateServiceCredential="true"/>
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<binding name="WSHttpBinding_ICommonService1">
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" establishSecurityContext="false" negotiateServiceCredential="true"/>
</security>
</binding>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
****************************************
注意: 每个WCF服务必须配置binding以及endpoint,本文仅配置ICommonService服务
****************************************
四、客户端测试
透明代理工厂动态创建基于Windows认证HTTP协议的WCF服务
C# Code:
private void button14_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", "123456");
//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", "123456");
//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,基础架构更成熟、应用更广泛、性能更稳定、开发效率更高!


本文关联文章:
WCF顶级安全:HTTPS协议SSL证书+Windows认证 +
TransportWithMessageCredential安全模式
http://www.csframework.com/archive/1/arc-1-20210816-3749.htm
WCF开发框架-客户端采用Windows身份认证调用HTTPS协议WCF接口
http://www.csframework.com/archive/1/arc-1-20210813-3743.htm
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
http://www.csframework.com/archive/1/arc-1-20210816-3749.htm
WCF开发框架-客户端采用Windows身份认证调用HTTPS协议WCF接口
http://www.csframework.com/archive/1/arc-1-20210813-3743.htm
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

扫一扫加作者微信


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