案例下载

http://download.csdn.net/detail/woxpp/4113172

客户端调用代码 通过代理类

代理生成 参见

http://www.cnblogs.com/woxpp/p/6232298.html

X509证书创建

http://www.cnblogs.com/woxpp/p/6232325.html

自定义用户名密码验证需要证书的支持

服务器端配置代码

  1. <system.serviceModel>
  2. <services>
  3. <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="CustomBehavior">
  4. <host>
  5. <baseAddresses>
  6. <add baseAddress="http://localhost:8732/WcfServiceLibrary"/>
  7. </baseAddresses>
  8. </host>
  9. <!-- Service Endpoints -->
  10. <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
  11. <endpoint address="net.tcp://localhost:8731/WcfServiceLibrary" binding="netTcpBinding" bindingConfiguration="TestNetTcpBinding" contract="WcfServiceLibrary1.IService1">
  12. <!--
  13. 部署时,应删除或替换下列标识元素,以反映
  14. 用来运行所部署服务的标识。删除之后,WCF 将
  15. 自动推断相应标识。
  16. -->
  17. <identity>
  18. <dns value="localhost"/>
  19. </identity>
  20. </endpoint>
  21. <!-- Metadata Endpoints -->
  22. <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
  23. <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
  24. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  25. </service>
  26. </services>
  27. <bindings>
  28. <netTcpBinding>
  29. <binding name="TestNetTcpBinding">
  30. <security mode="Message">
  31. <message clientCredentialType="UserName"/>
  32. </security>
  33. </binding>
  34. </netTcpBinding>
  35. </bindings>
  36. <behaviors>
  37. <serviceBehaviors>
  38. <behavior name="CustomBehavior">
  39. <!-- 为避免泄漏元数据信息,
  40. 请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
  41. <serviceMetadata httpGetEnabled="True"/>
  42. <!-- 要接收故障异常详细信息以进行调试,
  43. 请将以下值设置为 true。在部署前设置为 false
  44. 以避免泄漏异常信息-->
  45. <serviceDebug includeExceptionDetailInFaults="False" />
  46. <serviceCredentials>
  47. <serviceCertificate findValue="TestServer" storeName="My" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>
  48. <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceLibrary1.MyCustomValidator,WcfServiceLibrary1"/>
  49. </serviceCredentials>
  50. </behavior>
  51. </serviceBehaviors>
  52. </behaviors>
  53. </system.serviceModel>

服务端自定义验证代码

  1. namespace WcfServiceLibrary1
  2. {
  3. public class MyCustomValidator : UserNamePasswordValidator
  4. {
  5. public override void Validate(string userName, string password)
  6. {
  7. // validate arguments
  8. if (string.IsNullOrEmpty(userName))
  9. throw new ArgumentNullException("userName");
  10. if (string.IsNullOrEmpty(password))
  11. throw new ArgumentNullException("password");
  12. // check if the user is not xiaozhuang
  13. if (userName != "abcd1234" || password != "abcd1234")
  14. throw new SecurityTokenException("用户名或者密码错误!");
  15. }
  16. }
  17. }
  1. private void btnTest_Click(object sender, EventArgs e)
  2. {
  3. //Service1Client client = new Service1Client();
  4. //txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
  5.  
  6. NetTcpBinding binding2 = new NetTcpBinding();
  7. binding2.Security.Mode = SecurityMode.Message;
  8. binding2.Security.Message = new MessageSecurityOverTcp() { ClientCredentialType = MessageCredentialType.UserName };
  9. EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://localhost:8731/WcfServiceLibrary"),
  10. EndpointIdentity.CreateDnsIdentity("TestServer"));
  11. ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding2, endpoint);
  12. factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My,
  13. X509FindType.FindBySubjectName, "TestServer");
  14. factory.Credentials.UserName.UserName = "abcd1234";
  15. factory.Credentials.UserName.Password = "abcd1234";
  16. IService1 client = factory.CreateChannel();
  17. txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
  18. //B9DF5B912B8CF8EAB07A7BB9B0D17694522AB0CE
  19. }

WCF 安全性之 自定义用户名密码验证的更多相关文章

  1. 【WCF】Silverlight+wcf+自定义用户名密码验证

    本文摘自 http://www.cnblogs.com/virusswb/archive/2010/01/26/1656543.html 在昨天的博文Silverlight3+wcf+在不使用证书的情 ...

  2. 【WCF】使用“用户名/密码”验证的合理方法

    我不敢说俺的方法是最佳方案,反正这世界上很多东西都是变动的,正像老子所说的——“反(返)者,道之动”.以往看到有些文章中说,为每个客户端安装证书嫌麻烦,就直接采用把用户名和密码塞在SOAP头中发送,然 ...

  3. 自定义实现wcf的用户名密码验证

    目前wcf分为[传输层安全][消息层安全]两种,本身也自带的用户名密码验证的功能,但是ms为了防止用户名密码明文在网络上传输,所以,强制要求一旦使用[用户名密码]校验功能,则必须使用证书,按照常理讲, ...

  4. WCF服务安全控制之netTcpBinding的用户名密码验证【转】

    选择netTcpBinding WCF的绑定方式比较多,常用的大体有四种: wsHttpBinding basicHttpBinding netTcpBinding wsDualHttpBinding ...

  5. WCF用户名密码验证方式

    WCF使用用户名密码验证 服务契约 namespace WCFUserNameConstract { [ServiceContract] public interface IWcfContract { ...

  6. OpenVPN使用用户名/密码验证方式

    OpenVPN推荐使用证书进行认证,安全性很高,但是配置起来很麻烦.还好它也能像pptp等vpn一样使用用户名/密码进行认证. 不管何种认证方式,服务端的ca.crt, server.crt, ser ...

  7. WebService 用户名密码验证

    原文:WebService 用户名密码验证 在项目开发的过程中,WebService是经常要用的,当调用WebService方法时,需要经过服务的验证才可以调用,一般就是用户名/密码验证,还有一个就是 ...

  8. Python实现LDAP用户名密码验证

    网上借鉴了不少东西,下面是python代码,备份后用. 思路,因为每个用户的组都不一样,这样就导致了dn不一致的情况, 据需要先根据用户名获取该用户的dn,然后再bind用户名和密码进行验证. 反正是 ...

  9. IdentityServer4 学习笔记[2]-用户名密码验证

    回顾 上一篇介绍了IdentityServer4客户端授权的方式,今天来看看IdentityServer4的基于密码验证的方式,与客户端验证相比,主要是配置文件调整一下,让我们来看一下 配置修改 pu ...

随机推荐

  1. supervisor-1:基础篇

    别人博客转载,做个记录 原文链接:http://lixcto.blog.51cto.com/4834175/1539136 有阵子没写博客了,这段时间一直在研究python django框架和前端相关 ...

  2. java.lang.NoSuchMethodError: android.view.View.setBackground

    int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { ...

  3. 常用的js正则表达式

    正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑.熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升. 下面是一些,在前端开 ...

  4. Python 爬虫5——爬取并下载网页指定规格的图片

    看完上篇文档之后,我们对于正则表达式已经有了基本的了解,其实学习最有效的办法就是带着问题和目的,这里我们假设有一个目标:获取某个网页上指定规格的图片的链接地址,并下载到本地. 一.实现步骤: 1.在浏 ...

  5. css制作对话框

    当你发现好多图都能用css画出来的时候,你就会觉得css很有魅力了.//我是这么觉得的,先不考虑什么兼容问题 像漫画里出现的对话框,往往都是一个对话框然后就加入一个箭头指向说话的那一方,来表示这个内容 ...

  6. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

  7. Flask 重新认识

    总是觉的学习东西有点猴子掰玉米的感觉.今天就重新再掰一次吧. Installation: 安装之前建议先安装virtualenv,这个东东是帮助你在多个python版本之间保持同步,不至于python ...

  8. 定时任务crontab 例子

    查看定时任务格式 [root@centos ~]# vim /etc/crontab 1 SHELL=/bin/bash 2 PATH=/sbin:/bin:/usr/sbin:/usr/bin 3 ...

  9. *HDU 1398 母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  10. .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口

    对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...