2009-12-18 11 views
10

Tôi có hai dịch vụ WCF RESTful - dịch vụ "chung" là công khai và không có bảo mật; dịch vụ "quản trị" tôi định sử dụng xác thực cơ bản qua SSL. Đây là tôi web.config phía máy chủ:Trong WCF, cho một webHttpBinding, làm cách nào để xác định thông tin đăng nhập trong web.config phía máy khách khi máy chủ đang sử dụng xác thực cơ bản?

<system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
      <binding name="general" maxReceivedMessageSize="2147483647"> 
       <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" /> 
       <security mode="None"> 
        <transport clientCredentialType="None" /> 
       </security> 
      </binding> 
      <binding name="admin" maxReceivedMessageSize="2147483647"> 
       <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" /> 
       <security mode="Transport"> 
        <transport clientCredentialType="Basic" /> 
       </security> 
      </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="web"> 
       <webHttp/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service name="MyNamespace.AppServices.GeneralService"> 
      <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IGeneralService" behaviorConfiguration="web" bindingConfiguration="general" /> 
     </service> 
     <service name="MyNamespace.AppServices.AdminService"> 
      <endpoint address="" binding="webHttpBinding" contract="MyNamespace.Contracts.IAdminService" behaviorConfiguration="web" bindingConfiguration="admin" /> 
     </service> 
    </services> 
</system.serviceModel> 

Về phía khách hàng, Tôi hiện đang có code mà trông như thế này:

private static IGeneralService GetGeneralChannel() 
{ 
    WebHttpBinding binding = new WebHttpBinding(); 
    binding.Security.Mode = WebHttpSecurityMode.None; 
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 
    binding.MaxReceivedMessageSize = Int32.MaxValue; 
    binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; 
    binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; 

    WebChannelFactory<IGeneralService> cf = new WebChannelFactory<IGeneralService>(binding, new Uri("http://localhost:1066/GeneralService")); 
    IGeneralService channel = cf.CreateChannel(); 
    return channel; 
} 

private static IAdminService GetAdminChannel() 
{ 
    WebHttpBinding binding = new WebHttpBinding(); 
    binding.Security.Mode = WebHttpSecurityMode.Transport; 
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
    binding.MaxReceivedMessageSize = Int32.MaxValue; 
    binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; 
    binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; 

    WebChannelFactory<IAdminService> cf = new WebChannelFactory<IAdminService>(binding, new Uri("http://localhost:1066/AdminService")); 
    cf.Credentials.UserName.UserName = "myUserName"; 
    cf.Credentials.UserName.Password = "myPassword"; 

    IAdminService channel = cf.CreateChannel(); 
    return channel; 
} 

Câu hỏi đặt ra là, vì tôi rõ ràng là không muốn hard- mã tất cả thông tin cấu hình này, làm thế nào để tôi cần cung cấp nó trong web.config trên máy khách? Nó là khá rõ ràng với tôi rằng các yếu tố ràng buộc cần phải nhìn khá giống nhau trên máy khách như nó trên máy chủ. Tuy nhiên, nơi nào tôi chỉ ra các thông tin được gán cho WebChannelFactory?

Mọi trợ giúp và/hoặc thông tin chi tiết sẽ được đánh giá cao.

Cảm ơn, Steve

Trả lời

7

Bạn không thể đặt những thông tin (username và password) vào web.config và đã WCF đọc chúng từ đó. Đây là một trong số ít tính năng trong WCF mà không thể thực hiện được trong cấu hình - bạn phải thiết lập các thông tin đăng nhập đó trong mã của bạn.

Tất nhiên, trong mã của bạn, bạn có thể đọc chúng từ ví dụ: một bảng cơ sở dữ liệu, hoặc một mục cấu hình ở đâu đó - nhưng bạn phải tự làm điều đó. WCF không thể được cấu hình để tự động đọc các thiết lập từ một nơi nào đó.

+0

có thể giải thích tại sao, sau khi tìm kiếm khắp nơi, tôi không thể tìm thấy bất kỳ thứ gì giống như tên người dùng/mật khẩu trong lược đồ cấu hình wcf - he he cảm ơn thông tin. thật dễ dàng để đặt các giá trị này trong phần tử appSettings. –