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

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的崆峒網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
UserName認(rèn)證機(jī)制很簡(jiǎn)單,客戶端提供用戶名密碼信息,到服務(wù)器端通過(guò)UserName驗(yàn)證類進(jìn)行驗(yàn)證。在此過(guò)程中,需要X509證書的支持,使用X509證書并不是用于證書認(rèn)證而是使用X509證書的密鑰對(duì)用戶名密碼進(jìn)行加密以防在服務(wù)器上以明文方式傳遞。
測(cè)試時(shí)我們可以通過(guò)VS命令行創(chuàng)建測(cè)試使用的證書,如下:
C:\Program Files\Microsoft Visual Studio 9.0\VC>makecert.exe -sr LocalMachine -s
s My -a sha1 -n CN=SecurityTest -sky exchange –pe
然后我們需要編寫一個(gè)驗(yàn)證用戶名密碼的類,如下:
- Imports System.IdentityModel.Selectors
- Public Class MyCustomValidator
- Inherits UserNamePasswordValidator
- Public Overrides Sub Validate
(ByVal userName As String,
ByVal password As String)- ''驗(yàn)證過(guò)程
- End Sub
- End Class
服務(wù)器端的web.config文件還需要增加一些配置,如下:
"SecurityHost.Service1Behavior" - name="SecurityHost.Service1">
"wsHttpBinding" bindingConfiguration
="mySecureBinding"- contract="SecurityHost.IService1">
"mexHttpBinding" contract=
"IMetadataExchange" />DetailInFaults="false" /> "SecurityTest" storeLocation="LocalMachine" - storeName="My" x509FindType=
"FindBySubjectName" />userNamePasswordValidationMode="Custom" - customUserNamePasswordValidatorType=
"ClassLibrary1.MyCustomValidator,ClassLibrary1" />
客戶端進(jìn)行服務(wù)引用之后,可通過(guò)如下代碼指定身份信息:
- Dim client As New ServiceReference1.Service1Client
- '' 我們是使用X509證書密鑰加密并非進(jìn)行證書認(rèn)證
client.ClientCredentials.Service
Certificate.Authentication.Certificate
ValidationMode = ServiceModel.
Security.X509CertificateValidationMode.None- '' 指定客戶端身份:用戶名、密碼
- client.ClientCredentials.UserName
.UserName = Guid.NewGuid.ToString- client.ClientCredentials.UserName
.Password = Guid.NewGuid.ToString- '' 執(zhí)行服務(wù)方法
Dim str As String = client.GetData(1)
這樣我們就可以進(jìn)行WCF服務(wù)的UserName認(rèn)證了。
網(wǎng)頁(yè)標(biāo)題:WCF認(rèn)證之UserName認(rèn)證方法
文章地址:http://m.5511xx.com/article/cdihgij.html


咨詢
建站咨詢
