2014-03-25 2 views
0

나는 사용자 이름과 암호 및 인증서를 사용하여 wcf 서비스를 보호하는 자습서를 사용하지 않았습니다. 복수형의 자체 Cert 도구를 사용하여 인증서를 만들고 설치했습니다. 메서드를 확인합니다.이 메서드는 UserNamePasswordValidator에서 상속됩니다. 인증서 보안이 제대로 작동하지만 유효성 검사 메소드가 서비스 호출 중에 호출되지 않습니다. 서비스는 사용자 이름과 암호를 제공하지 않고 액세스 할 수 있습니다.
나는 그것에 관한 많은 기사를 읽었지만 동일한 절차가 거기에 제시되어있다. 나도 같은 문제에 대한 다른 스택 오버플로 질문을 읽을 수 있지만 그것을위한 해결책을 찾을 수 없습니다.

인증 코드 여기WCF가 UserNamePasswordValidator에서 유효성 검사 메서드를 호출 할 수 없습니다.

using System; 
using System.IdentityModel.Selectors; 
using System.ServiceModel; 

namespace WcfSecure 
{ 
    public class CredentialValidator : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      if (userName == null && password == null) 
       throw new ArgumentNullException(); 
      if (!(userName == "one" && password == "two")) 
       throw new FaultException("Wrong Credentials!"); 
     } 
    } 
} 

서비스 계약.

using System.ServiceModel; 

namespace WcfSecure 
{ 

    [ServiceContract] 
    public interface ISecureWebService 
    { 
     [OperationContract] 
     int SecureAdd(int x, int y); 

     [OperationContract] 
     int UnSecureService(int x, int y); 
    } 
} 

서비스 코드

namespace WcfSecure 
{ 
    public class SecureWebService : ISecureWebService 
    { 
     public int SecureAdd(int x, int y) 
     { 
      return x + y; 
     } 

     public int UnSecureService(int x, int y) 
     { 
      return x + y; 
     } 
    } 
} 

그리고

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 


    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 


    <system.serviceModel> 

    <bindings> 
     <wsHttpBinding> 
     <binding name="SecureBinding"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" establishSecurityContext="true"/> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 

    <behaviors> 
     <serviceBehaviors > 
     <behavior name="CustomBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
      <serviceCredentials> 
      <serviceCertificate findValue="SecureService" 
           storeLocation="LocalMachine" 
           storeName="My" 
           x509FindType="FindBySubjectName"/> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfSecure.CredentialValidator, WcfSecure" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <services> 
     <service behaviorConfiguration="CustomBehavior" name ="WcfSecure.SecureWebService"> 
     <endpoint address="" binding="wsHttpBinding" contract="WcfSecure.ISecureWebService"></endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://192.168.1.224:84/WcfSecure/SecureWebService"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 

    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 
+0

왜 사용자가 자신의 유효성 검사기를 구현하는지 잘 모르겠습니다. WCF 서비스는 사용자 이름/암호 유효성 검사가 포함 된 인증서로 보안되며 WCF 클래스를 만질 필요가 없습니다. – DeanOC

+0

작동 원리 또는 데모 코드를 알려주십시오. – vikrantx

답변

0

가) 내 인증서가 모두 설치 한 가장 중요한의 Web.config 클라이언트와 서버 기계

B) 이것은 클라이언트 구성 파일의 섹션입니다.

<system.serviceModel> 
    <bindings> 
     <ws2007HttpBinding> 
     <binding name="WS2007HttpBinding_MyAppWCFServices" 
       closeTimeout="00:00:30" 
       openTimeout="00:00:30" 
       receiveTimeout="00:00:30" 
       sendTimeout="00:05:00" 
       bypassProxyOnLocal="false" 
       transactionFlow="false" 
       hostNameComparisonMode="StrongWildcard" 
       maxBufferPoolSize="6000000" 
       maxReceivedMessageSize="6000000" 
       messageEncoding="Text" 
       textEncoding="utf-8" 
       useDefaultWebProxy="true" 
       allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="6000000" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> 
      <security mode="Message"> 
      <transport realm="" /> 
      <message clientCredentialType="UserName" negotiateServiceCredential="false" /> 
      </security> 
     </binding> 
     </ws2007HttpBinding> 
    </bindings> 

    <behaviors> 
     <endpointBehaviors> 
     <behavior name="MyAppServiceBehaviour"> 
      <clientCredentials> 
      <serviceCertificate> 
       <authentication certificateValidationMode="None" revocationMode="NoCheck"/> 
      </serviceCertificate> 
      </clientCredentials> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <client> 
     <endpoint address="http://servername:888/MyApp.WCF.Services/WorkFlowService.svc" 
     binding="ws2007HttpBinding" bindingConfiguration="WS2007HttpBinding_MyAppWCFServices" behaviorConfiguration="MyAppServiceBehaviour" 
     contract="MyApp.WCF.Services.IWorkFlowService" name="WorkFlowServiceEndpoint"> 
     <identity> 
      <certificateReference storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" findValue="AF43F4486E52B225408B252C6479DBD6814FCE5B"/> 
     </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

C)이 서버 구성 파일

<system.serviceModel> 
    <bindings> 
     <ws2007HttpBinding> 
     <binding name="WS2007HttpBinding_MyAppWCFServices" 
       closeTimeout="00:00:30" 
       openTimeout="00:00:30" 
       receiveTimeout="00:01:00" 
       sendTimeout="00:10:00" 
       bypassProxyOnLocal="false" 
       transactionFlow="false" 
       hostNameComparisonMode="StrongWildcard" 
       maxBufferPoolSize="6000000" 
       maxReceivedMessageSize="6000000" 
       messageEncoding="Text" 
       textEncoding="utf-8" 
       useDefaultWebProxy="true" 
       allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="6000000" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> 
      <security mode="Message"> 
      <transport realm="" /> 
      <message clientCredentialType="UserName" negotiateServiceCredential="false" /> 
      </security> 
     </binding> 
     </ws2007HttpBinding> 
    </bindings> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"> 
     <serviceActivations> 
     <add relativeAddress="MyApp.WCF.Services/WorkflowService.svc" service="MyApp.WCF.Services.WorkFlowService" /> 
     </serviceActivations> 
    </serviceHostingEnvironment> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MyAppServiceBehaviour"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true" /> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <serviceCredentials> 
      <serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" findValue="AF43F4486E52B225408B252C6479DBD6814FCE5B" /> 
      </serviceCredentials> 
     </behavior> 
     <behavior name="ExposeMetaDataBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <services> 
     <service behaviorConfiguration="MyAppServiceBehaviour" name="MyApp.WCF.Services.WorkFlowService"> 
     <endpoint address="" binding="ws2007HttpBinding" bindingConfiguration="WS2007HttpBinding_MyAppWCFServices" contract="MyApp.WCF.Services.IWorkFlowService" /> 
     </service> 
    </services> 
    </system.serviceModel> 

이 도움이 희망의 섹션입니다!