2016-04-29 16 views
14

Tôi có một dịch vụ WCF Tôi đã tạo ra trong một WebApplication với cấu hình như sau trong web.configBắt DefaultNetworkCredentials đi qua để WCF Service

<service name="RedwebServerManager.WebApplication.DesktopService" 
      behaviorConfiguration="ServBehave"> 
    <endpoint 
     address="" 
     binding="basicHttpBinding" 
     bindingConfiguration="basicBind" 
     contract="RedwebServerManager.WebApplication.IDesktopService"/> 
    </service> 
</services> 
<bindings> 
    <basicHttpBinding> 
    <binding name="basicBind"> 
     <security mode="TransportWithMessageCredential"> 
     <transport clientCredentialType="Windows"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

Dịch vụ này cần thực hiện trong WindowsCredentials trong để có được thông tin trong một cơ sở dữ liệu dựa trên người dùng đã được xác thực. Đây dịch vụ hiện tại có một phương pháp thực hiện một giao diện với các chữ ký sau

[ServiceContract] 
public interface IDesktopService 
{ 
    /// <summary> 
    /// Gets the clients. 
    /// </summary> 
    /// <returns>IEnumerable&lt;ClientServiceModel&gt;.</returns> 
    [OperationContract] 
    IEnumerable<ClientServiceModel> GetClients(); 
} 

Tôi có một ứng dụng Windows Forms được tiêu thụ các dịch vụ WCF và tôi muốn đi qua các thông tin của người dùng hiện bằng cách sử dụng ứng dụng như điều này sẽ tất cả đều ngồi trên miền của chúng tôi. App.config như sau

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IDesktopService"> 
       <security mode="TransportWithMessageCredential"> 
       <transport clientCredentialType="Windows" proxyCredentialType="Windows"/> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://redwebservermanager.redweb.network/DesktopService.svc" 
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDesktopService" 
      contract="ManagerService.IDesktopService" name="BasicHttpBinding_IDesktopService" /> 
    </client> 
</system.serviceModel> 

Nếu tôi tự thiết lập các thông tin username và mật khẩu mọi thứ hoạt động một cách chính xác tuy nhiên tôi đã cố gắng

managerService.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials 

để tôi có thể vượt qua các chứng chỉ người dùng hiện tại trên nhưng tôi tiếp tục nhận được một lỗi trên các cuộc gọi đến GetClients() mà tên người dùng và mật khẩu không được thiết lập.

Có ai có thể giúp tôi không? Tôi cũng đã thử thêm

[OperationBehavior(Impersonation = ImpersonationOption.Required)] 

vào phương pháp nhưng điều này không có sự khác biệt.

+0

Tôi biết điều đó nghe có vẻ ngớ ngẩn, nhưng bạn đã thử ** không đặt ** 'ClientCredential' chút nào. Chúng tôi đang sử dụng 'NetTcpBinding' với bảo mật giao thông và các công trình trên cho chúng tôi. –

Trả lời

6

Bạn có thể nhận được Username (và biết thêm thông tin khác với mật khẩu) bằng một trong các cách sau:

//1. 
System.Security.Principal.WindowsIdentity.GetCurrent(); 
//2.  
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
    WindowsPrincipal user = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal; 
//3.  
WindowsIdentity ident = WindowsIdentity.GetCurrent(); 
    WindowsPrincipal user = new WindowsPrincipal(ident); 

Nhưng, không có cách nào bạn có thể truy cập vào mật khẩu của người dùng. Vui lòng tham khảo này question

Khi bạn nhận được thông tin nhận dạng, bạn có thể vượt qua các tên người dùng để WCF sử dụng OutgoingMessageHeaders, như:

MessageHeader<string> header = new MessageHeader<string>(userName); 
     OperationContextScope contextScope = new OperationContextScope(InnerChannel); 
     OperationContext.Current.OutgoingMessageHeaders.Add(
     header.GetUntypedHeader("String", "System")); 

Và trong việc thực hiện dịch vụ WCF bạn có thể đọc nó thích:

OperationContext context = OperationContext.Current; 

      if (context != null) 
      { 
       try 
       { 
        _LoginName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("String", "System");   
       } 
       catch 
       { 
        _LoginName = string.Empty; 
       } 
      } 

Vui lòng cho tôi biết nếu điều này giúp hoặc nếu bạn có bất kỳ câu hỏi nào khác.

Cảm ơn bạn, Soma.

Các vấn đề liên quan