查看网站国际联网备案号,专业商城网站建设价格低,凡客诚品盈利模式,网页设计培训机构多少钱1. Transfer SecurityTransfer Security 主要包括三个方面#xff1a; 消息完整性(Message Integrity)、消息机密性 (Message Confidentiality) 和 交互验证(Mutual Authentication)。消息完整性必须确保消息在传输过程中没有被篡改…1. Transfer SecurityTransfer Security 主要包括三个方面 消息完整性(Message Integrity)、消息机密性 (Message Confidentiality) 和 交互验证(Mutual Authentication)。消息完整性必须确保消息在传输过程中没有被篡改接收的消息是完整且正确的消息机密性必须确保消息不会被任何第三方查阅消息内容不会泄漏给任何非相关人员而交互认证则是指客户端和服务器必须通过某种信任机制才能建立正确的连接同时交互认证还要监测并阻止拒绝服务攻击(DOS)。通常的做法是对消息进行数字签名来确保其完整性使用非对称加密算法来阻止消息内容外泄而用户名/密码、X.509 数字证书等方式则可以用来验证对方身份,在这里我们主要讲述如何在WCF中使用用户名/密码的身份验证方式. 2. 创建x.509数字证书: 要使用userName/password方式来验证身份,我们需要为服务器装一个证书,创建证书的作用是因为用户名和密码在client和service传输的过程中需要加密,否则就没有安全性了,x.509rd使用非对称加密加技术.用公钥加密客户端用户名和密码,在服务端用私钥来解密,所以我们得创建这样的证书.使用vs2008的tool中的command命令下执行:makecert -r -pe -n CNTemp -ss My -sky exchange .我们就可以为服务器生成Temp的证书.如下图所示. 点击view可以查看详细信息: X.509 简单证书介绍: X.509给出的鉴别框架是一种基于公开密钥体制的鉴别业务密钥管理。一个用户有两把密钥一把是用户的专用密钥另一把是其他用户都可利用 的公共密钥。用户可用常规密钥如DES为信息加密然后再用接收者的公共密钥对DES进行加密并将之附于信息之上这样接收者可用对应的专用密钥 打开DES密锁并对信息解密。该鉴别框架允许用户将其公开密钥存放在它的目录款项中。一个用户如果想与另一个用户交换秘密信息就可以直接从对方 的目录款项中获得相应的公开密钥用于各种安全服务。更多的可以参考MSDN 3.Solution 结构如下图: solution:WCFValidationContract WCF contract. WCFValidationClient WCF client端. WCFValidationServices 主要是实现contract的类 WCFValidationHost..用于启动WCF. 创建服务 [ServiceContract] public interface IUserName//对应solution中的WCFValidationContract.IUserName { [OperationContract] bool test(); } public class UserName:IUserName//对应solution中的WCFValidationServices.UserName { #region IUserName Members public bool test() { return true; } #endregion } 我们通过继承 UserNamePasswordValidator 来创建一个自定义验证器。//对应solution中的WCFValidationServices.MyValidation public override void Validate(string userName, string password) { //the follow code is testing only.u can read userName and password from DataBase. if (userName ! user || password ! pwd) { throw new Exception(Unknown Username or Password); } } 服务端的配置文件如下:(host的配置文件) ?xml version1.0 encodingutf-8 ?configuration system.serviceModel behaviors serviceBehaviors behavior nameUserNameBehavior serviceMetadata httpGetEnabledtrue/ serviceDebug includeExceptionDetailInFaultstrue/ serviceCredentials issuedTokenAuthentication allowUntrustedRsaIssuerstrue/issuedTokenAuthentication clientCertificate authentication certificateValidationModeNone/ /clientCertificate serviceCertificate findValueTemp storeLocationCurrentUser x509FindTypeFindBySubjectName/ userNameAuthentication userNamePasswordValidationModeCustom customUserNamePasswordValidatorTypeWCFValidationServices.MyValidation,WCFValidationServices/ /serviceCredentials /behavior /serviceBehaviors /behaviors bindings wsHttpBinding binding nameuserBinding security modeMessage message clientCredentialTypeUserName/ /security /binding /wsHttpBinding /bindings services service behaviorConfigurationUserNameBehavior nameWCFValidationServices.UserName endpoint address bindingwsHttpBinding bindingConfigurationuserBinding name username contractWCFValidation.IUserName /endpoint host baseAddresses add baseAddresshttp://localhost/userName/ /baseAddresses /host /service /services /system.serviceModel/configuration 有时候我们在启动服务的时候会生产如下的错误: 这时我们需要使用微软件提供的WCF Samples中的一个工具FindPrivateKey来长到Temp的私钥文件的位置,然后为ASPNET或NET Service用户分配访问权限WCF Samples下载地址 http://download.csdn.net/source/792492也可以到MSDN上下载 FindPrivateKey的使用介始: http://msdn.microsoft.com/zh-cn/vbasic/aa717039.aspx 4. 创建客户端启动服务器后创建客户端代理文件。注意自动生成的客户端配置文件中包含了服务器数字证书的相关信息。 ?xml version1.0 encodingutf-8?configuration system.serviceModel bindings wsHttpBinding binding nameusername closeTimeout00:01:00 openTimeout00:01:00 receiveTimeout00:10:00 sendTimeout00:01:00 bypassProxyOnLocalfalse transactionFlowfalse hostNameComparisonModeStrongWildcard maxBufferPoolSize524288 maxReceivedMessageSize65536 messageEncodingText textEncodingutf-8 useDefaultWebProxytrue allowCookiesfalse readerQuotas maxDepth32 maxStringContentLength8192 maxArrayLength16384 maxBytesPerRead4096 maxNameTableCharCount16384 / reliableSession orderedtrue inactivityTimeout00:10:00 enabledfalse / security modeMessage transport clientCredentialTypeWindows proxyCredentialTypeNone realm / message clientCredentialTypeUserName negotiateServiceCredentialtrue algorithmSuiteDefault establishSecurityContexttrue / /security /binding /wsHttpBinding /bindings client endpoint addresshttp://localhost/userName bindingwsHttpBinding bindingConfigurationusername contractIUserName nameusername identity certificate encodedValueAwAAAAEAAAAUAAAAqxw7daba6mcItJ/tKIAFZfz3TCggAAAAAQAAAPcBAAAwggHzMIIBXKADAgECAhAciN6VY5e8ik4b7Ia5KvftMA0GCSqGSIb3DQEBBAUAMBMxETAPBgNVBAMTCE15U2VydmVyMB4XDTA4MTEyMDA2MTQwNloXDTM5MTIzMTIzNTk1OVowEzERMA8GA1UEAxMITXlTZXJ2ZXIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALK4gy7ldnVwSjemT3bQjKSEGd/zBjNqYDHf9kwUopwvuHpE287yWD1ytKaYYZf7uEdEtNYKwWeOwSNLEPqxUSW4jF92IqfQwkxa0bQdZQK/Y3TpmseX/hsOtW0FBXV3FtqqacrWVkG/J/HFM1eeIyPggVI/QqclrKjBQeikjMdAgMBAAGjSDBGMEQGA1UdAQQ9MDuAEEvEZkpRY3c664NiQiazM3hFTATMREwDwYDVQQDEwhNeVNlcnZlcoIQHIjelWOXvIpOGyGuSr37TANBgkqhkiG9w0BAQQFAAOBgQAQLrEU3mnxOlDkKuVx9OatXd0w99I3xMnQOsWvOCITjQrfUeJWz1FOl46pKAXDhJNgfMW133E3ARgUxfabkJqz9ejhjvzJwx2CJYe843h98fooTPPbSs6rQrfQOxb/KoaxbKoxUaALQsGssEXkN2ImS0jsOUm9aVNnRNWpKMhzA / /identity /endpoint /client /system.serviceModel/configuration 以配置文件是由svcutil工具生成的.我们在客户端写以下代码来调用服务: UserNameClient aa new UserNameClient(); aa.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode System.ServiceModel.Security.X509CertificateValidationMode.None; aa.ClientCredentials.UserName.UserName user; aa.ClientCredentials.UserName.Password pwd; bool flag aa.test(); Console.WriteLine(flag.ToString()); Console.ReadKey(); 这时我们可以在控制应用程序中显示True这个值. 当我们输入一个错误的密码时如: aa.ClientCredentials.UserName.UserName 123; aa.ClientCredentials.UserName.Password 123; 客户端就会有异常: 这样我们不可以完成了WCF的 userName/password的验证方式,在项目中使用时,我们不必要在每次调用方法的时候设置用户名和密码,我们只要写一个基类就可以了 转载于:https://www.cnblogs.com/liujiang/archive/2008/11/21/1338384.html