2012-11-05 140 views
0

在這裏,我正在WCF RESTfull服務中實現自定義usernamepasswordvalidator。我需要的是通過Chrome REST客戶端調用此http://localhost:12229/RestServiceImpl.svc/GetStudentObj時,它不驗證用戶名密碼..直接調用此方法並獲取結果。我在做什麼錯誤這裏??使用basicHttpBinding和自定義UserNamePasswordValidator進行WCF身份驗證?

我的界面

[ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", UriTemplate = "/GetStudentObj", RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json)] 
     Student GetStudent(); 
    } 

SVC

public class RestServiceImpl : IRestServiceImpl 
    { 
     public Student GetStudent() 
     { 
       Student stdObj = new Student 
       { 
        StudentName = "Foo", 
        Age = 29, 
        Mark = 95 
       }; 
       return stdObj; 

     } 

     public class CustomUserNameValidator : UserNamePasswordValidator 
     { 
      public override void Validate(string userName, string password) 
      { 

       if (null == userName || null == password) 
       { 
        throw new ArgumentNullException("You must provide both the username and password to access this service"); 
       } 

       if (!(userName == "user1" && password == "test") && !(userName == "user2" && password == "test")) 
       { 

        throw new SecurityTokenException("Unknown Username or Incorrect Password"); 
       } 
      } 
     } 

    } 

和程序Web.Config

<system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpTransportSecurity"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Basic"></transport> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <protocolMapping> 
     <add scheme="http" binding="webHttpBinding"/> 
    </protocolMapping> 
    <services> 
     <service name="RestService.RestServiceImpl" > 
     <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl"></endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior > 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="SecureRESTSvcTestBehavior"> 
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="RESTfulSecuritySH.CustomUserNameValidator, RESTfulSecuritySH" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

任何建議?

+0

Downvoted - 你說你想使用basicHttpBinding的,但你使用的wsHttpBinding來代替。 – stephen

回答

0

您似乎沒有爲您的服務設置自定義行爲。

您需要使用behaviorConfiguration屬性來告訴你SecureRESTSvcTestBehavior問題的服務:

<service name="RestService.RestServiceImpl" behaviorConfiguration="SecureRESTSvcTestBehavior"> 
    <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl"></endpoint> 
</service> 
相關問題