日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

WCF Security userName/Password

發(fā)布時間:2023/12/18 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF Security userName/Password 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. Transfer Security
Transfer Security 主要包括三個方面: "消息完整性(Message Integrity)"、"消息機密性 (Message Confidentiality)" 和 "交互驗證(Mutual Authentication)"。
消息完整性必須確保消息在傳輸過程中沒有被篡改,接收的消息是完整且正確的;消息機密性必須確保消息不會被任何第三方查閱,消息內(nèi)容不會泄漏給任何非相關(guān)人員;而交互認證則是指客戶端和服務(wù)器必須通過某種信任機制才能建立正確的連接,同時交互認證還要監(jiān)測并阻止拒絕服務(wù)攻擊(DOS)。通常的做法是對消息進行數(shù)字簽名來確保其完整性,使用非對稱加密算法來阻止消息內(nèi)容外泄,而用戶名/密碼、X.509 數(shù)字證書等方式則可以用來驗證對方身份,在這里我們主要講述如何在WCF中使用用戶名/密碼的身份驗證方式.

2. 創(chuàng)建x.509數(shù)字證書:

要使用userName/password方式來驗證身份,我們需要為服務(wù)器裝一個證書,創(chuàng)建證書的作用是因為用戶名和密碼在client和service傳輸?shù)倪^程中需要加密,否則就沒有安全性了,x.509rd使用非對稱加密加技術(shù).用公鑰加密客戶端用戶名和密碼,在服務(wù)端用私鑰來解密,所以我們得創(chuàng)建這樣的證書.使用vs2008的tool中的command命令下執(zhí)行:makecert -r -pe -n "CN=Temp" -ss My -sky exchange??.我們就可以為服務(wù)器生成Temp的證書.如下圖所示.

點擊view可以查看詳細信息:

?

?X.509 簡單證書介紹:

  X.509給出的鑒別框架是一種基于公開密鑰體制的鑒別業(yè)務(wù)密鑰管理。一個用戶有兩把密鑰:一把是用戶的專用密鑰,另一把是其他用戶都可利用 的公共密鑰。用戶可用常規(guī)密鑰(如DES)為信息加密,然后再用接收者的公共密鑰對DES進行加密并將之附于信息之上,這樣接收者可用對應(yīng)的專用密鑰 打開DES密鎖,并對信息解密。該鑒別框架允許用戶將其公開密鑰存放在它的目錄款項中。一個用戶如果想與另一個用戶交換秘密信息,就可以直接從對方 的目錄款項中獲得相應(yīng)的公開密鑰,用于各種安全服務(wù)。更多的可以參考MSDN 3.Solution 結(jié)構(gòu)如下圖: solution:WCFValidationContract WCF contract.      WCFValidationClient WCF client端.      WCFValidationServices 主要是實現(xiàn)contract的類      WCFValidationHost..用于啟動WCF. 創(chuàng)建服務(wù)
??? [ServiceContract]
??? public interface IUserName//對應(yīng)solution中的WCFValidationContract.IUserName
??? {
??????? [OperationContract]
??????? bool test();
??? }
??? public class UserName:IUserName//對應(yīng)solution中的WCFValidationServices.UserName
??? {
??????? #region IUserName Members
??????? public bool test()
??????? {
??????????? return true;
??????? }
??????? #endregion
??? }
我們通過繼承 UserNamePasswordValidator 來創(chuàng)建一個自定義驗證器。//對應(yīng)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");
??????????? }
??????? }
服務(wù)端的配置文件如下:(host的配置文件) <?xml version="1.0" encoding="utf-8" ?>
<configuration>
? <system.serviceModel>
??? <behaviors>
????? <serviceBehaviors>
??????? <behavior name="UserNameBehavior">
????????? <serviceMetadata httpGetEnabled="true"/>
????????? <serviceDebug includeExceptionDetailInFaults="true"/>
????????? <serviceCredentials>
??????????? <issuedTokenAuthentication allowUntrustedRsaIssuers="true"></issuedTokenAuthentication>
??????????? <clientCertificate>
????????????? <authentication certificateValidationMode="None"/>
??????????? </clientCertificate>
??????????? <serviceCertificate findValue="Temp" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>
??????????? <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFValidationServices.MyValidation,WCFValidationServices"/>
????????? </serviceCredentials>
??????? </behavior>
????? </serviceBehaviors>
??? </behaviors>
??? <bindings>
????? <wsHttpBinding>
??????? <binding name="userBinding">
????????? <security mode="Message">
??????????? <message clientCredentialType="UserName"/>
????????? </security>
??????? </binding>
????? </wsHttpBinding>
??? </bindings>
??? <services>
????? <service behaviorConfiguration="UserNameBehavior" name="WCFValidationServices.UserName">
??????? <endpoint address="" binding="wsHttpBinding" bindingConfiguration="userBinding" name?? ="username" contract="WCFValidation.IUserName">
????????? </endpoint>
??????? <host>
????????? <baseAddresses>
??????????? <add baseAddress="http://localhost/userName"/>
????????? </baseAddresses>
??????? </host>
????? </service>
??? </services>
? </system.serviceModel>
</configuration>
有時候我們在啟動服務(wù)的時候會生產(chǎn)如下的錯誤: 這時我們需要使用微軟件提供的WCF Samples中的一個工具FindPrivateKey來長到Temp的私鑰文件的位置,然后為ASPNET或NET Service用戶分配訪問權(quán)限
WCF Samples下載地址 http://download.csdn.net/source/792492也可以到MSDN上下載
FindPrivateKey的使用介始: http://msdn.microsoft.com/zh-cn/vbasic/aa717039.aspx 4. 創(chuàng)建客戶端

啟動服務(wù)器后,創(chuàng)建客戶端代理文件。注意自動生成的客戶端配置文件中包含了服務(wù)器數(shù)字證書的相關(guān)信息。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
? <system.serviceModel>
??? <bindings>
????? <wsHttpBinding>
??????? <binding name="username" closeTimeout="00:01:00" openTimeout="00:01:00"
??????????? receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
??????????? transactionFlow="false" hostNameComparisonMode="StrongWildcard"
??????????? maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
??????????? messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
??????????? allowCookies="false">
????????? <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
????????????? maxBytesPerRead="4096" maxNameTableCharCount="16384" />
????????? <reliableSession ordered="true" inactivityTimeout="00:10:00"
????????????? enabled="false" />
????????? <security mode="Message">
??????????? <transport clientCredentialType="Windows" proxyCredentialType="None"
??????????????? realm="" />
??????????? <message clientCredentialType="UserName" negotiateServiceCredential="true"
??????????????? algorithmSuite="Default" establishSecurityContext="true" />
????????? </security>
??????? </binding>
????? </wsHttpBinding>
??? </bindings>
??? <client>
????? <endpoint address="http://localhost/userName" binding="wsHttpBinding"
????????? bindingConfiguration="username" contract="IUserName" name="username">
??????? <identity>
????????? <certificate encodedValue="AwAAAAEAAAAUAAAAqxw7daba6mcItJ/tKIAFZfz3TCggAAAAAQAAAPcBAAAwggHzMIIBXKADAgECAhAciN6VY5e8ik4b7Ia5KvftMA0GCSqGSIb3DQEBBAUAMBMxETAPBgNVBAMTCE15U2VydmVyMB4XDTA4MTEyMDA2MTQwNloXDTM5MTIzMTIzNTk1OVowEzERMA8GA1UEAxMITXlTZXJ2ZXIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALK4gy7ldnVwSjemT3bQjKSEGd/zBjNqYDHf9kwUopwvuHpE287yWD1ytKaYYZf7uEdEtNYKwWeOwSNLEPqxUSW4jF92IqfQwkxa0bQdZQK/Y3TpmseX/hsOtW0FBXV3Ftqq+acrWVkG/J/HFM1eeIyPggVI/QqclrKjBQeikjMdAgMBAAGjSDBGMEQGA1UdAQQ9MDuAEEvEZkpRY3c664NiQiazM3+hFTATMREwDwYDVQQDEwhNeVNlcnZlcoIQHIjelWOXvIpOG+yGuSr37TANBgkqhkiG9w0BAQQFAAOBgQAQLrEU3mnxOlDkKuVx9OatXd0w99I3xMnQOsWvOCITjQrfUeJWz1FOl46pKAXDhJNgfMW133E3ARgUxf+abkJqz9ejhjvzJwx2CJYe843h98fooTPPbSs6rQrfQOxb/KoaxbKoxUaALQsGssEXkN2ImS0jsOUm9aVNnRNWpKMhzA==" />
??????? </identity>
????? </endpoint>
??? </client>
? </system.serviceModel>
</configuration>
以配置文件是由svcutil工具生成的.我們在客戶端寫以下代碼來調(diào)用服務(wù): ? 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();
這時我們可以在控制應(yīng)用程序中顯示True這個值. 當我們輸入一個錯誤的密碼時如:
?aa.ClientCredentials.UserName.UserName = "123";
??aa.ClientCredentials.UserName.Password = "123";
客戶端就會有異常: ?
? ?這樣我們不可以完成了WCF的 userName/password的驗證方式,在項目中使用時,我們不必要在每次調(diào)用方法的時候設(shè)置用戶名和密碼,我們只要寫一個基類就可以了 ? ?

轉(zhuǎn)載于:https://www.cnblogs.com/liujiang/archive/2008/11/21/1338384.html

總結(jié)

以上是生活随笔為你收集整理的WCF Security userName/Password的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。