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

WCF認(rèn)證之UserName認(rèn)證方法

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF認(rèn)證在實(shí)際應(yīng)用中是一個(gè)比較重要的部分。其中的UserName認(rèn)證機(jī)制比較簡(jiǎn)單,我們可以使用X509證書(shū)密鑰對(duì)用戶名密碼進(jìn)行加密。

WCF認(rèn)證的主要作用是幫助我們實(shí)現(xiàn)安全的開(kāi)發(fā)環(huán)境。在這里我們就為大家介紹一下WCF認(rèn)證中的一個(gè)叫做UserName認(rèn)證的實(shí)現(xiàn)方法。#t#

UserName認(rèn)證機(jī)制很簡(jiǎn)單,客戶端提供用戶名密碼信息,到服務(wù)器端通過(guò)UserName驗(yàn)證類(lèi)進(jìn)行驗(yàn)證。在此過(guò)程中,需要X509證書(shū)的支持,使用X509證書(shū)并不是用于證書(shū)認(rèn)證而是使用X509證書(shū)的密鑰對(duì)用戶名密碼進(jìn)行加密以防在服務(wù)器上以明文方式傳遞。

 

測(cè)試時(shí)我們可以通過(guò)VS命令行創(chuàng)建測(cè)試使用的證書(shū),如下:
C:\Program Files\Microsoft Visual Studio 9.0\VC>makecert.exe -sr LocalMachine -s
s My -a sha1 -n CN=SecurityTest -sky exchange –pe

然后我們需要編寫(xiě)一個(gè)驗(yàn)證用戶名密碼的類(lèi),如下:

 

  1. Imports System.IdentityModel.Selectors  
  2. Public Class MyCustomValidator  
  3. Inherits UserNamePasswordValidator  
  4. Public Overrides Sub Validate
    (ByVal userName As String, 
    ByVal password As String)  
  5. ''驗(yàn)證過(guò)程  
  6. End Sub  
  7. End Class 

 

服務(wù)器端的web.config文件還需要增加一些配置,如下:

 

  1. <system.serviceModel> 
  2. <bindings> 
  3. <wsHttpBinding> 
  4. <!-- 設(shè)置綁定名稱(chēng) --> 
  5. <binding name="mySecureBinding"> 
  6. <security mode="Message"> 
  7. <!-- 設(shè)置客戶端身份類(lèi)型 --> 
  8. <message clientCredentialType="UserName"/> 
  9. </security> 
  10. </binding> 
  11. </wsHttpBinding> 
  12. </bindings> 
  13. <services> 
  14. <service behaviorConfiguration=
    "SecurityHost.Service1Behavior" 
  15. name="SecurityHost.Service1"> 
  16. <endpoint address="" binding=
    "wsHttpBinding" bindingConfiguration
    ="mySecureBinding" 
  17. contract="SecurityHost.IService1"> 
  18. <identity> 
  19. <!-- 使用以證書(shū)一致的DNS名稱(chēng) --> 
  20. <dns value="SecurityTest" /> 
  21. </identity> 
  22. </endpoint> 
  23. <endpoint address="mex" binding=
    "mexHttpBinding" contract=
    "IMetadataExchange" /> 
  24. </service> 
  25. </services> 
  26. <behaviors> 
  27. <serviceBehaviors> 
  28. <behavior name="SecurityHost.Service1Behavior"> 
  29. <serviceMetadata httpGetEnabled="true" /> 
  30. <serviceDebug includeException
    DetailInFaults
    ="false" /> 
  31. <!-- 配置服務(wù)器身份 --> 
  32. <serviceCredentials> 
  33. <!-- 證書(shū)類(lèi)型 --> 
  34. <serviceCertificate findValue=
    "SecurityTest" storeLocation="LocalMachine" 
  35. storeName="My" x509FindType=
    "FindBySubjectName" /> 
  36. <!-- 自定義驗(yàn)證類(lèi) --> 
  37. <userNameAuthentication 
    userNamePasswordValidationMode="Custom" 
  38. customUserNamePasswordValidatorType=
    "ClassLibrary1.MyCustomValidator,ClassLibrary1" /> 
  39. </serviceCredentials> 
  40. </behavior> 
  41. </serviceBehaviors> 
  42. </behaviors> 
  43. </system.serviceModel> 

客戶端進(jìn)行服務(wù)引用之后,可通過(guò)如下代碼指定身份信息:

 

  1. Dim client As New ServiceReference1.Service1Client  
  2. '' 我們是使用X509證書(shū)密鑰加密并非進(jìn)行證書(shū)認(rèn)證
     
    client.ClientCredentials.Service
    Certificate.Authentication.Certificate
    ValidationMode
     = ServiceModel.
    Security.X509CertificateValidationMode.None  
  3. '' 指定客戶端身份:用戶名、密碼  
  4. client.ClientCredentials.UserName
    .UserName
     = Guid.NewGuid.ToString  
  5. client.ClientCredentials.UserName
    .Password
     = Guid.NewGuid.ToString  
  6. '' 執(zhí)行服務(wù)方法 

Dim str As String = client.GetData(1)
 

這樣我們就可以進(jìn)行WCF服務(wù)的UserName認(rèn)證了。

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

2011-07-14 11:12:47

限制BT

2009-12-21 14:58:57

WCF用戶密碼認(rèn)證

2011-07-14 11:12:39

Cisco限制BT

2011-07-14 11:03:22

2009-09-08 09:25:46

思科認(rèn)證學(xué)習(xí)方法思科認(rèn)證

2009-08-31 09:14:31

思科認(rèn)證思科CCIE認(rèn)證思科認(rèn)證高級(jí)認(rèn)證

2009-08-31 09:03:54

思科認(rèn)證初級(jí)認(rèn)證思科CCDA認(rèn)證

2009-08-24 10:22:17

思科認(rèn)證思科認(rèn)證分類(lèi)介紹

2023-06-16 08:13:57

2010-08-19 11:50:47

思科華為認(rèn)證

2013-08-15 13:41:53

Nginx用戶認(rèn)證

2010-08-19 11:54:38

思科華為認(rèn)證

2009-08-26 10:37:49

思科認(rèn)證CCNA思科認(rèn)證

2009-08-22 11:06:58

思科認(rèn)證介紹思科認(rèn)證注冊(cè)方法思科認(rèn)證

2009-09-07 09:28:00

思科認(rèn)證CCSPCCSP認(rèn)證過(guò)程

2009-08-28 09:45:07

最新思科認(rèn)證CCNA Securi

2009-12-07 14:47:21

Juniper認(rèn)證

2009-08-20 12:35:12

思科認(rèn)證思科認(rèn)證種類(lèi)思科認(rèn)證資料

2011-09-15 14:21:38

2009-09-04 09:37:49

思科認(rèn)證CCNA學(xué)習(xí)方法
點(diǎn)贊
收藏

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