排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
WCF
一:创建证书
使用vs的命令创建
makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=TestServer -sky exchange -pe
创建证书细节http://www.tnblog.net/aojiancc2/article/details/2554
二:wcf服务器端
接口:
namespace WcfCertificate { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ITcpCer”。 [ServiceContract(CallbackContract = typeof(ICallback))]//指定UserCallBack回调接口 public interface ITcpCer { [OperationContract] void DoWork(); } public interface ICallback //回调接口客服端实现 { [OperationContract(IsOneWay = true)]//单向调用,不需要返回值 void TestCallBack(string hello); } }
实现:
namespace WcfCertificate { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“TcpCer”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 TcpCer.svc 或 TcpCer.svc.cs,然后开始调试。 public class TcpCer : ITcpCer { public void DoWork() { OperationContext.Current.GetCallbackChannel<ICallback>().TestCallBack("回调了"); } } }
配置文件:
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mybehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <serviceCredentials> <clientCertificate> <authentication certificateValidationMode="None" /> </clientCertificate> <serviceCertificate findValue="TestServer" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfCertificate.Validator,WcfCertificate" /> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> <bindings> <netTcpBinding> <binding name="netTcpBindConfig" receiveTimeout="00:20:00"> <security mode="Message"> <message clientCredentialType="UserName" /> </security> </binding> </netTcpBinding> </bindings> <services> <service name="WcfCertificate.TcpCer" behaviorConfiguration="mybehavior"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBindConfig" contract="WcfCertificate.ITcpCer"/> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> </service> </services> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。 --> <directoryBrowse enabled="true"/> </system.webServer> </configuration>
三:客服端
namespace wfTcpCer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ServiceReference1.TcpCerClient tc = new ServiceReference1.TcpCerClient( new System.ServiceModel.InstanceContext(new CallBack())); tc.ClientCredentials.UserName.UserName = "sa"; tc.ClientCredentials.UserName.Password = "1234"; tc.DoWorkAsync();//有回调函数用异步调用方式 } public class CallBack : ServiceReference1.ITcpCerCallback //回调接口 { public void TestCallBack(string hello) { MessageBox.Show("text callback"); } } } }
配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_ITcpCer"> <security mode="Message"> <message clientCredentialType="UserName" /> </security> </binding> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://2011-20130817ae:4503/TcpCer.svc" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITcpCer" contract="ServiceReference1.ITcpCer" name="NetTcpBinding_ITcpCer"> <identity> <!--这个是自动生成的,也可以用下边的dns value="TestServer"因为certificateValidationMode="None"不需要验证证书--> <certificate encodedValue="AwAAAAEAAAAUAAAA3j+VKPLILWzCfoG9VXc2yhchi+kgAAAAAQAAADoCAAAwggI2MIIB5KADAgECAhDmLEMHisejr0j4qDS+rn+HMAkGBSsOAwIdBQAwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3kwHhcNMTMxMjMxMDM0MjE4WhcNMzkxMjMxMjM1OTU5WjAVMRMwEQYDVQQDEwpUZXN0U2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtXJzwFZrUgBRsLu5c8wt0iEwj/EcI9IFHK8tAraRtKsLtoWi3CHxrwi39rNaWh0SN11w8QX+0rEwAdxoxppGPPTTMbRMlcUHWHZ8oTn5V66nzYds0X2tt993KV0k1YX1ZXDuA37LZFROwksqCYfsl2sAenVkiLizzjrMVaIUPuzVvaphpxd+KcivPAYdNW/tCgC1vracULEVnu+86uPhyT9dUykIByB4rhbR0EHUNuw/4CtTsx+a3Wqn/vhPgjy08nxx+wfB/ekJmNnoz0cNVobq4WOpExDNZdGrrow9qVMcIvcGj53vGb4BhzdCwZWzf0U/K3LAJEqg+2/P+HlcPwIDAQABo0swSTBHBgNVHQEEQDA+gBAS5AktBh0dTwCNYSHcFmRjoRgwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3mCEAY3bACqAGSKEc+41KpcNfQwCQYFKw4DAh0FAANBABjcWxmn9cVILyozv4cVvGQJ6ZfAen7pHI1MV1dZ/YhJHd1Ou6dHHafAI3755TyY7gpEHPu4xuRN6eXwYg2FkvI=" /> <!--<dns value="TestServer"/>--> </identity> </endpoint> </client> <behaviors> <endpointBehaviors> <behavior name="mye"> <clientCredentials> <serviceCertificate> <!--这里必须要制定为None--> <authentication certificateValidationMode="None"/> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价