自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在實(shí)際應(yīng)用中實(shí)現(xiàn)WCF用戶(hù)密碼認(rèn)證

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF用戶(hù)密碼認(rèn)證在實(shí)際應(yīng)用中是比較常用的,不過(guò)它的實(shí)現(xiàn)方法對(duì)于剛剛接觸WCF的朋友來(lái)說(shuō)還是比較困難的。在這里就向大家詳細(xì)介紹這些知識(shí)。

WCF框架是一款功能強(qiáng)大的分布式開(kāi)發(fā)框架。對(duì)于初學(xué)者來(lái)說(shuō),可能其中有些功能不太熟悉。這需要我們?cè)诓粩嗟膶?shí)踐中去慢慢研究這些功能。比如WCF用戶(hù)密碼認(rèn)證的正確使用。#t#

以前我們用WebService做分布式系統(tǒng)的時(shí)候,認(rèn)證是個(gè)麻煩的問(wèn)題,通常的做法是繼承一個(gè)SoapHeader,把用戶(hù)名和密碼放到里面,每調(diào)用一個(gè)方法都要把用戶(hù)名和密碼傳遞給服務(wù)器端來(lái)驗(yàn)證 ,效率相當(dāng)?shù)?,代碼編寫(xiě)相當(dāng)?shù)穆闊疫€不安全!

WCF支持多種認(rèn)證技術(shù),例如Windowns認(rèn)證、X509證書(shū)、Issued Tokens、用戶(hù)名密碼認(rèn)證等,在跨Windows域分布的系統(tǒng)中,用戶(hù)名密碼認(rèn)證還是比較常用的,要實(shí)現(xiàn)用戶(hù)名密碼認(rèn)證,就必須需要X509證書(shū),為什么呢?因?yàn)槲覀冃枰猉509證書(shū)這種非對(duì)稱(chēng)密鑰技術(shù)來(lái)實(shí)現(xiàn)WCF在Message傳遞過(guò)程中的加密和解密,要不然用戶(hù)名和密碼就得在網(wǎng)絡(luò)上明文傳遞!詳細(xì)說(shuō)明就是客戶(hù)端把用戶(hù)名和密碼用公鑰加密后傳遞給服務(wù)器端,服務(wù)器端再用自己的私鑰來(lái)解密,然后傳遞給相應(yīng)的驗(yàn)證程序來(lái)實(shí)現(xiàn)身份驗(yàn)證。

當(dāng)然,做個(gè)測(cè)試程序就沒(méi)有必要去申請(qǐng)一個(gè)X509數(shù)字簽名證書(shū)了,微軟提供了一個(gè)makecert.exe的命令專(zhuān)門(mén)用來(lái)生成測(cè)試使用的X509證書(shū)的,那我們就來(lái)建立一個(gè)測(cè)試用的證書(shū),在cmd下輸入以下命令:

makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=MyServerCert -sky exchange –pe

這個(gè)命令的意思就是創(chuàng)建一個(gè)測(cè)試的X509證書(shū),這個(gè)證書(shū)放在存儲(chǔ)位置為'Localmachine'的'My'這個(gè)文件夾下,證書(shū)主題名字叫'MyServerCert',需要更多關(guān)于makecert命令的信息請(qǐng)參考MSDN。

證書(shū)建立好了,我們就可以編寫(xiě)代碼了,在VS2008下建立一個(gè)解決方案并在上面建立兩個(gè)Web項(xiàng)目,一個(gè)是'Asp.net Web 應(yīng)用程序'(客戶(hù)端),一個(gè)是'WCF服務(wù)應(yīng)用程序'(服務(wù)器端),我們先來(lái)編寫(xiě)服務(wù)器端代碼,首先我們要編寫(xiě)自己的WCF用戶(hù)密碼認(rèn)證邏輯,先要在WCF項(xiàng)目上添加引用'System.IdentityModel'然后我們建立一個(gè)新的類(lèi)文件并繼承自'System.IdentityModel.Selectors.UserNamePasswordValidator',然后我們重寫(xiě)里面的'Validate'方法來(lái)實(shí)現(xiàn)用戶(hù)名密碼認(rèn)證邏輯。代碼如下;

 

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.IdentityModel.Selectors;   
  6. using System.IdentityModel.Tokens;   
  7. namespace ServerWcfService.CustomValidators   
  8. {   
  9. public class MyCustomValidator : 
    UserNamePasswordValidator   
  10. {   
  11. /// < summary>   
  12. /// Validates the user name and 
    password combination.   
  13. /// < /summary>   
  14. /// < param name="userName">
    The user name.< /param>   
  15. /// < param name="password">
    The password.< /param>   
  16. public override void Validate
    (string userName, string password)   
  17. {   
  18. // validate arguments   
  19. if (string.IsNullOrEmpty(userName))   
  20. throw new ArgumentNullException("userName");   
  21. if (string.IsNullOrEmpty(password))   
  22. throw new ArgumentNullException("password");   
  23. // check if the user is not xiaozhuang   
  24. if (userName != "xiaozhuang" || password != "123456")   
  25. throw new SecurityTokenException("用戶(hù)名或者密碼錯(cuò)誤!");   
  26. }   
  27. }   
  28. }  

 

上面只是一個(gè)簡(jiǎn)單的WCF用戶(hù)密碼認(rèn)證,實(shí)際應(yīng)用中用戶(hù)名和密碼一般都保存在數(shù)據(jù)庫(kù)中,如果驗(yàn)證不通過(guò)就拋出一個(gè)'SecurityTokenException'類(lèi)型的異常;下一步我們需要配置一下服務(wù)端的webConfig文件,我的WebConfig文件Servicemodel配置節(jié)如下:

 

  1. < system.serviceModel>   
  2. < bindings>   
  3. < wsHttpBinding>   
  4. < binding name="mySecureBinding">   
  5. < security mode="Message">   
  6. < message clientCredentialType="UserName"/>   
  7. < /security>   
  8. < /binding>   
  9. < /wsHttpBinding>   
  10. < /bindings>   
  11. < services>   
  12. < service behaviorConfiguration=
    "ServerWcfService.Services.MySimple
    ServiceBehavior"
     name="ServerWcfService.
    Services.MySimpleService"
    >   
  13. < endpoint address="" binding=
    "wsHttpBinding" contract="ServerWcfService.
    ServiceContracts.IMySimpleService"
     bindingConfiguration="mySecureBinding">   
  14. < identity>   
  15. < dns value="MyServerCert"/>   
  16. < /identity>   
  17. < /endpoint>   
  18. < endpoint address="mex" binding=
    "mexHttpBinding" contract="IMetadataExchange"/>   
  19. < /service>   
  20. < /services>   
  21. < behaviors>   
  22. < serviceBehaviors>   
  23. < behavior name="ServerWcfService.
    Services.MySimpleServiceBehavior"
    > 
  24. < serviceMetadata httpGetEnabled="true"/>   
  25. < serviceDebug includeExceptionDetailInFaults="false"/>   
  26. < serviceCredentials>   
  27. < serviceCertificate findValue=
    "MyServerCert" x509FindType="FindBySubjectName" 
    storeLocation="LocalMachine" storeName="My"/>   
  28. < userNameAuthentication userNamePassword
    ValidationMode
    ="Custom" customUserName
    PasswordValidatorType
    ="ServerWcfService.
    CustomValidators.MyCustomValidator,ServerWcfService"
    />   
  29. < /serviceCredentials>   
  30. < /behavior>   
  31. < /serviceBehaviors>   
  32. < /behaviors>   
  33. < /system.serviceModel>  

 

加粗的那些是我加上去的或者在默認(rèn)上修改了的。Bindings節(jié)指定了客戶(hù)端提供的認(rèn)證類(lèi)型為'username'并在endpoint節(jié)中指定bianding配置。在dns節(jié)中修改原來(lái)的localmachine為MyServerCert,當(dāng)然你也可以修改為別的,這取決于你的證書(shū)主題名稱(chēng)是什么。也就是上面命令中的CN=MyServerCert,接下來(lái)我們加入在serviceCredentials配置節(jié),并在里面配置兩個(gè)小節(jié),ServiceCertificate節(jié)中指定了我們的X509證書(shū)的位置,以用來(lái)加解密message,usernameAuthentication節(jié)中指定了我們自己的WCF用戶(hù)密碼認(rèn)證邏輯。

Sorry,忘了一件事情,就是寫(xiě)一個(gè)測(cè)試的服務(wù)契約并實(shí)現(xiàn),寫(xiě)法上和無(wú)認(rèn)證的寫(xiě)法一樣,如下

  1. ServerWcfService.Service
    Contracts.IMySimpleService:   
  2. [OperationContract]   
  3. string PrintMessage
    (string message);  

 

這樣,服務(wù)端的代碼編寫(xiě)和配置就完成了,生成項(xiàng)目測(cè)試一下,頁(yè)面顯示服務(wù)已生成成功。

接下來(lái)我們開(kāi)始編寫(xiě)客戶(hù)端代碼,先在客戶(hù)端引用剛才生成的WCF服務(wù),然后編寫(xiě)客戶(hù)端代碼如下:

 

  1. protected void btnPrint_Click(object 
    sender, EventArgs e)   
  2. {   
  3. TestWCFService.MySimpleServiceClient 
    client = new ClientWeb.TestWCFService.
    MySimpleServiceClient();   
  4. client.ClientCredentials.UserName.
    UserName
     = "xiaozhuang";   
  5. client.ClientCredentials.UserName.
    Password
     = "123456";   
  6. lbMessage.Text = client.PrintMessage
    (txtMessage.Text);   
  7. }  

 

 

如果你有一個(gè)真正的X509證書(shū),那么現(xiàn)在的WCF用戶(hù)密碼認(rèn)證代碼就可以正常運(yùn)行了。但是很不幸,我們的證書(shū)是測(cè)試用的,我們運(yùn)行的時(shí)候出錯(cuò):'X.509 certificate CN=MyServerCert 鏈生成失敗。所使用的證書(shū)具有無(wú)法驗(yàn)證的信任鏈。請(qǐng)?zhí)鎿Q該證書(shū)或更改 certificateValidationMode。已處理證書(shū)鏈,但是在不受信任提供程序信任的根證書(shū)中終止',WCF無(wú)法驗(yàn)證測(cè)試證書(shū)的信任鏈,那我們要做的就是繞過(guò)這個(gè)信任驗(yàn)證,具體做法如下:

先要在Asp.net Web應(yīng)用程序項(xiàng)目上添加引用'System.IdentityModel'然后我們建立一個(gè)新的類(lèi)文件并繼承自'System.IdentityModel.Selectors.X509CertificateValidator',然后我們重寫(xiě)里面的'Validate'方法來(lái)實(shí)現(xiàn)我們自己的X509認(rèn)證邏輯,代碼如下:

 

  1. using System;   
  2. using System.Configuration;   
  3. using System.IdentityModel.Selectors;   
  4. using System.IdentityModel.Tokens;   
  5. using System.Security.Cryptography.
    X509Certificates;   
  6. namespace ClientWeb.CustomX509Validator   
  7. {   
  8. /// < summary>   
  9. /// Implements the validator for X509
     certificates.   
  10. /// < /summary>   
  11. public class MyX509Validator: 
    X509CertificateValidator   
  12. {   
  13. /// < summary>   
  14. /// Validates a certificate.   
  15. /// < /summary>   
  16. /// < param name="certificate">
    The certificate the validate.< /param>   
  17. public override void Validate
    (X509Certificate2 certificate)   
  18. {   
  19. // validate argument   
  20. if (certificate == null)   
  21. throw new ArgumentNullException
    ("X509認(rèn)證證書(shū)為空!");   
  22. // check if the name of the certifcate matches   
  23. if (certificate.SubjectName.Name != 
    ConfigurationManager.AppSettings["CertName"])   
  24. throw new SecurityTokenValidationException(
    "Certificated was not issued by thrusted issuer");   
  25. }   
  26. }   
  27. }  

 

你可以把Validate方法里面留空讓所有的認(rèn)證都通過(guò),也可以自己定義認(rèn)證邏輯,如果認(rèn)證失敗,就拋出'SecurityTokenValidationException'的異常,然后我們配置一下客戶(hù)端的webconfig讓它使用我們自己的X509認(rèn)證,增加以下的配置節(jié),并在'endpoint'節(jié)中指定behaviorConfiguration="myClientBehavior"。

 

  1. < behaviors>   
  2. < endpointBehaviors>   
  3. < behavior name="myClientBehavior">   
  4. < clientCredentials>   
  5. < serviceCertificate>   
  6. < authentication certificateValidationMode=
    "Custom" customCertificateValidatorType=
    "ClientWeb.CustomX509Validator.
    MyX509Validator,ClientWeb"
     />   
  7. < /serviceCertificate>   
  8. < /clientCredentials>   
  9. < /behavior>   
  10. < /endpointBehaviors>   
  11. < /behaviors>  

 

 

OK,客戶(hù)端代碼和配置完成,現(xiàn)在你可以運(yùn)行自己的WCF用戶(hù)密碼認(rèn)證程序了。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-23 10:25:29

2010-03-01 10:45:59

WCF集合類(lèi)

2010-02-25 17:22:39

WCF服務(wù)行為

2010-03-02 16:43:46

2017-06-07 10:55:17

VMwareNSX應(yīng)用

2010-03-01 13:06:49

WCF繼承

2010-02-22 14:53:17

WCF用戶(hù)密碼

2009-12-21 14:49:27

2010-02-26 10:56:06

WCF Stream

2010-03-02 17:35:20

WCF服務(wù)加載

2010-02-22 13:28:05

WCF異步調(diào)用

2010-02-24 14:05:08

WCF openati

2010-03-01 17:52:03

WCF選擇綁定

2010-11-25 10:05:22

Visual StudSilverlightWCF

2009-11-03 11:03:00

CDN接入技術(shù)

2024-02-27 16:27:42

物聯(lián)網(wǎng)IOT智能連接

2021-08-28 10:06:29

VueJavascript應(yīng)用

2010-01-06 15:21:00

軟交換技術(shù)

2010-02-26 14:19:03

WCF用戶(hù)驗(yàn)證

2023-09-28 11:42:15

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)