WCF 安全性之 自定义用户名密码验证
案例下载
http://download.csdn.net/detail/woxpp/4113172
客户端调用代码 通过代理类
代理生成 参见
http://www.cnblogs.com/woxpp/p/6232298.html
X509证书创建
http://www.cnblogs.com/woxpp/p/6232325.html
自定义用户名密码验证需要证书的支持
服务器端配置代码
- <system.serviceModel>
- <services>
- <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="CustomBehavior">
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8732/WcfServiceLibrary"/>
- </baseAddresses>
- </host>
- <!-- Service Endpoints -->
- <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
- <endpoint address="net.tcp://localhost:8731/WcfServiceLibrary" binding="netTcpBinding" bindingConfiguration="TestNetTcpBinding" contract="WcfServiceLibrary1.IService1">
- <!--
- 部署时,应删除或替换下列标识元素,以反映
- 用来运行所部署服务的标识。删除之后,WCF 将
- 自动推断相应标识。
- -->
- <identity>
- <dns value="localhost"/>
- </identity>
- </endpoint>
- <!-- Metadata Endpoints -->
- <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
- <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- </service>
- </services>
- <bindings>
- <netTcpBinding>
- <binding name="TestNetTcpBinding">
- <security mode="Message">
- <message clientCredentialType="UserName"/>
- </security>
- </binding>
- </netTcpBinding>
- </bindings>
- <behaviors>
- <serviceBehaviors>
- <behavior name="CustomBehavior">
- <!-- 为避免泄漏元数据信息,
- 请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
- <serviceMetadata httpGetEnabled="True"/>
- <!-- 要接收故障异常详细信息以进行调试,
- 请将以下值设置为 true。在部署前设置为 false
- 以避免泄漏异常信息-->
- <serviceDebug includeExceptionDetailInFaults="False" />
- <serviceCredentials>
- <serviceCertificate findValue="TestServer" storeName="My" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>
- <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceLibrary1.MyCustomValidator,WcfServiceLibrary1"/>
- </serviceCredentials>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
服务端自定义验证代码
- namespace WcfServiceLibrary1
- {
- public class MyCustomValidator : UserNamePasswordValidator
- {
- public override void Validate(string userName, string password)
- {
- // validate arguments
- if (string.IsNullOrEmpty(userName))
- throw new ArgumentNullException("userName");
- if (string.IsNullOrEmpty(password))
- throw new ArgumentNullException("password");
- // check if the user is not xiaozhuang
- if (userName != "abcd1234" || password != "abcd1234")
- throw new SecurityTokenException("用户名或者密码错误!");
- }
- }
- }
- private void btnTest_Click(object sender, EventArgs e)
- {
- //Service1Client client = new Service1Client();
- //txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
- NetTcpBinding binding2 = new NetTcpBinding();
- binding2.Security.Mode = SecurityMode.Message;
- binding2.Security.Message = new MessageSecurityOverTcp() { ClientCredentialType = MessageCredentialType.UserName };
- EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://localhost:8731/WcfServiceLibrary"),
- EndpointIdentity.CreateDnsIdentity("TestServer"));
- ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding2, endpoint);
- factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My,
- X509FindType.FindBySubjectName, "TestServer");
- factory.Credentials.UserName.UserName = "abcd1234";
- factory.Credentials.UserName.Password = "abcd1234";
- IService1 client = factory.CreateChannel();
- txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
- //B9DF5B912B8CF8EAB07A7BB9B0D17694522AB0CE
- }
WCF 安全性之 自定义用户名密码验证的更多相关文章
- 【WCF】Silverlight+wcf+自定义用户名密码验证
本文摘自 http://www.cnblogs.com/virusswb/archive/2010/01/26/1656543.html 在昨天的博文Silverlight3+wcf+在不使用证书的情 ...
- 【WCF】使用“用户名/密码”验证的合理方法
我不敢说俺的方法是最佳方案,反正这世界上很多东西都是变动的,正像老子所说的——“反(返)者,道之动”.以往看到有些文章中说,为每个客户端安装证书嫌麻烦,就直接采用把用户名和密码塞在SOAP头中发送,然 ...
- 自定义实现wcf的用户名密码验证
目前wcf分为[传输层安全][消息层安全]两种,本身也自带的用户名密码验证的功能,但是ms为了防止用户名密码明文在网络上传输,所以,强制要求一旦使用[用户名密码]校验功能,则必须使用证书,按照常理讲, ...
- WCF服务安全控制之netTcpBinding的用户名密码验证【转】
选择netTcpBinding WCF的绑定方式比较多,常用的大体有四种: wsHttpBinding basicHttpBinding netTcpBinding wsDualHttpBinding ...
- WCF用户名密码验证方式
WCF使用用户名密码验证 服务契约 namespace WCFUserNameConstract { [ServiceContract] public interface IWcfContract { ...
- OpenVPN使用用户名/密码验证方式
OpenVPN推荐使用证书进行认证,安全性很高,但是配置起来很麻烦.还好它也能像pptp等vpn一样使用用户名/密码进行认证. 不管何种认证方式,服务端的ca.crt, server.crt, ser ...
- WebService 用户名密码验证
原文:WebService 用户名密码验证 在项目开发的过程中,WebService是经常要用的,当调用WebService方法时,需要经过服务的验证才可以调用,一般就是用户名/密码验证,还有一个就是 ...
- Python实现LDAP用户名密码验证
网上借鉴了不少东西,下面是python代码,备份后用. 思路,因为每个用户的组都不一样,这样就导致了dn不一致的情况, 据需要先根据用户名获取该用户的dn,然后再bind用户名和密码进行验证. 反正是 ...
- IdentityServer4 学习笔记[2]-用户名密码验证
回顾 上一篇介绍了IdentityServer4客户端授权的方式,今天来看看IdentityServer4的基于密码验证的方式,与客户端验证相比,主要是配置文件调整一下,让我们来看一下 配置修改 pu ...
随机推荐
- supervisor-1:基础篇
别人博客转载,做个记录 原文链接:http://lixcto.blog.51cto.com/4834175/1539136 有阵子没写博客了,这段时间一直在研究python django框架和前端相关 ...
- java.lang.NoSuchMethodError: android.view.View.setBackground
int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { ...
- 常用的js正则表达式
正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑.熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升. 下面是一些,在前端开 ...
- Python 爬虫5——爬取并下载网页指定规格的图片
看完上篇文档之后,我们对于正则表达式已经有了基本的了解,其实学习最有效的办法就是带着问题和目的,这里我们假设有一个目标:获取某个网页上指定规格的图片的链接地址,并下载到本地. 一.实现步骤: 1.在浏 ...
- css制作对话框
当你发现好多图都能用css画出来的时候,你就会觉得css很有魅力了.//我是这么觉得的,先不考虑什么兼容问题 像漫画里出现的对话框,往往都是一个对话框然后就加入一个箭头指向说话的那一方,来表示这个内容 ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
- Flask 重新认识
总是觉的学习东西有点猴子掰玉米的感觉.今天就重新再掰一次吧. Installation: 安装之前建议先安装virtualenv,这个东东是帮助你在多个python版本之间保持同步,不至于python ...
- 定时任务crontab 例子
查看定时任务格式 [root@centos ~]# vim /etc/crontab 1 SHELL=/bin/bash 2 PATH=/sbin:/bin:/usr/sbin:/usr/bin 3 ...
- *HDU 1398 母函数
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口
对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...