2010-10-25 106 views
8

我一直在爲WCF服務創建一個自定義的用戶名/密碼驗證器,並且運行了配置項customUserNamePasswordValidatorType。我已經能夠通過下面的例子使我的代碼工作,但我只是不明白髮生了什麼。不幸的是,MSDN article沒有提供太多細節。customUserNamePasswordValidatorType發生了什麼?

這是Microsoft提供的樣品:

<serviceCredentials> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" /> 
</serviceCredentials> 

我試圖瞭解這兩個參數是customUserNamePasswordValidatorType:「Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator」和「服務」。

有人可以幫我理解這些參數是什麼意思嗎?

謝謝!

回答

10

這第一個參數是該函數的自定義驗證的完全合格的名稱。第二個參數是組件的名稱功能包含英寸

從如何使用自定義的驗證器(略作修改,以適應你的例子)

namespace Microsoft.ServiceModel.Samples.CalculatorService 
{ 
    public class CustomUserNameValidator : UserNamePasswordValidator 
    { 
    // This method validates users. It allows in two users, 
    // test1 and test2 with passwords 1tset and 2tset respectively. 
    // This code is for illustration purposes only and 
    // MUST NOT be used in a production environment because it 
    // is NOT secure. 
    public override void Validate(string userName, string password) 
    { 
     if (null == userName || null == password) 
     { 
     throw new ArgumentNullException(); 
     } 

     if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) 
     { 
     throw new FaultException("Unknown Username or Incorrect Password"); 
     } 
     } 
    } 
} 

a much better example採取以上將內得到遵守程序集名爲service

+0

太棒了!謝謝! – Jacob 2010-10-25 21:13:43

6

第一部分是通過命名空間完全限定類名,第二個是組件的類是在。

+0

感謝您的幫助! – Jacob 2010-10-25 21:37:19

+0

直接準確,謝謝! – 2012-12-17 12:49:53