2009-08-18 4 views
7

WCF 서비스의 일부 끝점을 보안하려는 경우 일부 끝점과 일부 끝점을 보호 할 수 있는지 여부를 알 수 없습니다. 아래에는 WCF 서비스 (자체 호스팅 됨)가 제거되었습니다. 동일한 WCF는 CA 정책 파일에도 사용됩니다. 이 WCF 서비스 또는 일부 끝점 ut의 CA 정책 부분에 사용자 이름 암호를 묻지 않아야합니다. 항상 정책 파일에 액세스 할 수 있어야합니다. 그게 가능하니?사용자 지정 인증을 사용하여 WCF 서비스 끝점 보안

많은 WCF 맞춤 블로그/게시물을 발견했습니다. 보안을 수행 할 수있는 많은 방법이 있습니다. 필자가 원하는 것은 사용자 이름/암호로 일부 종점을 보호 할 수 있지만 피들러와 같은 도구로 자격 증명을 표시하면 안됩니다. 그러나이 경우 데이터를 볼 수 있습니다.

이미 Customvalidator를 구현했지만 app.config 파일도 가져올 항목을 정의합니다. 그리고 저는 그것에 능숙하지 않습니다.

namespace WindowsFormsApplication11 
{ 
    public partial class Form1 : Form 
    { 
     public ServiceHost _host = null; 

     public Form1() 
     { 
      InitializeComponent(); 
     }  

     private void button1_Click(object sender, EventArgs e) 
     { 
      // Create a ServiceHost for the CalculatorService type and 
      // provide the base address. 
      _host = new ServiceHost(typeof(WmsStatService)); 
      _host.AddServiceEndpoint(typeof(IPolicyProvider), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 

      _host.Open(); 
     } 
    } 

    // Define a service contract. 
    [ServiceContract(Namespace = "http://WindowsFormsApplication11")] 
    public interface IWmsStat 
    { 
     [OperationContract] 
     string getConnectedViewers(string channelName); 
     [OperationContract] 
     string sayHello(string name); 
    } 

    [ServiceContract] 
    public interface IPolicyProvider 
    { 
     [OperationContract, WebGet(UriTemplate = "/ClientAccessPolicy.xml")] 
     Stream ProvidePolicy(); 
    } 
    //[DataContract] 
    public class Ads 
    { 
     // [DataMember] 
     public string AdFileName { get; set; } 
     //[DataMember] 
     public string AdDestenationUrl { get; set; } 
     public string ConnectedUserIP { get; set; } 
    } 
    // 
    public class CustomValidator : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      if(null == userName || null == password) 
      { 
        throw new ArgumentNullException(); 
      } 
      if(userName == "Oguz" && password == "2009") 
      { 
       return; 
      } 
      FaultCode fc = new FaultCode("ValidationFailed"); 
      FaultReason fr = new FaultReason("Good reason"); 
      throw new FaultException(fr,fc); 
     } 
    } 
    // 

    public class WmsStatService : IWmsStat, IPolicyProvider 
    { 
     public string sayHello(string name) 
     { 
      return "hello there " + name + " nice to meet you!"; 
     } 

     public Stream ProvidePolicy() 
     { 
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml"; 
      return new MemoryStream(File.ReadAllBytes("ClientAccessPolicy.xml"), false); 
     } 

     public string getConnectedViewers(string channelname) 
     { 
      // do stuff 
      return null; 
     } 
    } 
} 

app.config. 이 설정 파일이 작동하지 않습니다. 엔드 포인트에 대한 사용자 정의 인증을 넣기를 원했습니다. 나는 단서가 없다.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="WindowsFormsApplication11.WmsStatService" behaviorConfiguration="mex"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://192.168.0.199:87" /> 
      </baseAddresses> 
     </host>   
     <endpoint address="http://192.168.0.199:87/Test" binding="basicHttpBinding" bindingConfiguration="" contract="WindowsFormsApplication11.IWmsStat" behaviorConfiguration="MyServiceBehavior" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <!--<bindings> 
     <wsHttpBinding>  
     <binding name="wshttp"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings>--> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="mex"> 
      <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     </behavior> 
     <behavior name="MyServiceBehavior"> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors>  
    </behaviors> 
    </system.serviceModel> 
</configuration> 

답변

17

나는 내가 당신을 일부 엔드 포인트 및 일부하지를 확보 할 수 있는지 알고 해달라고하는 WCF 서비스의 일부 엔드 포인트를 보호하려는. -

물론 당신은 다른 사람에, 다른 두 개의 별도의 바인딩 구성을 생성하고, 고정되어 그 엔드 포인트에서 하나를 사용해야합니다

<bindings> 
    <basicHttpBinding> 
    <binding name="secured"> 
     <security mode="Message"> 
     <message ...... /> 
     </security> 
    </binding> 
    <binding name="unsecured"> 
     <security mode="None" /> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<services> 
    <service name="WindowsFormsApplication11.WmsStatService" behaviorConfiguration="mex"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://192.168.0.199:87" /> 
     </baseAddresses> 
    </host>   

    <endpoint address="/Secured/Test" 
       binding="basicHttpBinding" bindingConfiguration="secured" 
       contract="WindowsFormsApplication11.IWmsStat" 
       behaviorConfiguration="MyServiceBehavior" /> 

    <endpoint address="/Unsecured/Test" 
       binding="basicHttpBinding" bindingConfiguration="unsecured" 
       contract="WindowsFormsApplication11.IWmsStat" 
       behaviorConfiguration="MyServiceBehavior" /> 

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

마크를

PS : 확실하지 그것이 게시 내용을 더 이상 최신으로 유지하지 못하는 문제인 경우 - 두 가지 별도의 동작 구성이 있다는 것을 눈치 채셨습니까?

<behaviors> 
    <serviceBehaviors> 
     <behavior name="mex"> 
     <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     </behavior> 
     <behavior name="MyServiceBehavior"> 
     <serviceCredentials> 
      <userNameAuthentication 
       userNamePasswordValidationMode="Custom" 
       customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
     </serviceCredentials> 
     </behavior> 
    </serviceBehaviors>  
</behaviors> 

귀하의 서비스는 "mex"행동만을 참조하고 있습니까? 즉, 귀하의 서비스는 실제로 <serviceMetadata> 행동을 사용하고 있습니다. - <serviceCredentials>!

<behaviors> 
    <serviceBehaviors> 
     <behavior name="Default"> 
     <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     <serviceCredentials> 
      <userNameAuthentication 
       userNamePasswordValidationMode="Custom" 
       customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
     </serviceCredentials> 
     </behavior> 
    </serviceBehaviors>  
</behaviors> 
<services> 
    <service name="...." behaviorConfiguration="Default" 

마크

+0

젠장, 저이 지금 :( 파일이나 어셈블리 '직접 CustomValidator'또는 해당 종속성 중 하나를로드 할 수 없습니다 무엇입니까 지정한 파일을 찾을 수 없습니다 – Shift

+0

을 그 오류는 여기이 설정에서 온다 userNamePasswordValidationMode = "사용자 지정"customUserNamePasswordValidatorType = "WindowsFormsApplication11.CustomValidator, 직접 CustomValidator"/> 은 서비스? –

+0

를 실행할 때 사용 가능한 "직접 CustomValidator"조립 내가 WindowsFormsAppiication11에.이 자체가 WCF를 호스팅하는 것으로 변경인가요 Windows Forms에서 서비스. 내가 그 오류가 사라 졌다고하지만 다른 문제는 사라 졌다고 변경 한 후 : ( 'http://192.168.0.199:87/Test'(계약서가있는 "IWmsStat")의 ChannelDispatcher가 IChannelListener를 열 수 없습니다. – Shift

2

전체 메시지를 보호하려면 전송 보안 모드를 사용해야합니다. 헤더 만 암호화/서명하려면 메시지 보안 모드에서 허용하지만 wsHttpBinding을 사용해야합니다. 다이제스트를 사용하여 자격 증명을 보호 할 수도 있습니다.

<bindings> 
    <basicHttpBinding> 
      <binding name="secure"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

또한 엔드 포인트 선언을 업데이트해야합니다 :

<endpoint 
    address="https://192.168.0.199:87/Test" 
    binding="basicHttpBinding" bindingConfiguration="secure" 
    contract="WindowsFormsApplication11.IWmsStat" /> 

당신은 허용되지 않습니다 귀하의 예로서

, 나는 당신의 주석 부분은 다음과 같이해야한다고 생각 전송 보안 모드와 함께 일반 HTTP를 사용합니다.

+0

을 나는 다음과 같은 오류가 발생하고 그 구성을 :

당신은 하나에 이러한 병합 후 그냥 참조 할 필요가있다. 'MyServiceBehavior'라는 끝점 동작이 없습니다 – Shift

+0

https를 사용하고 싶지 않으므로 메시지 보안 모드 – Shift

+0

을 선택했습니다. 서비스 태그 –

관련 문제