2016-04-29 65 views
14

我有我已經在web應用web.config中創造了以下配置WCF服務獲取DefaultNetworkCredentials通過對WCF服務

<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> 

這種服務需要採取WindowsCredentials在一個獲取信息基於認證用戶的數據庫。該服務目前有一個方法實現一個接口具有以下簽名

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

我有一個Windows窗體應用程序,它在消費WCF服務,我想使用的應用程序,因爲這樣會通過當前用戶的憑據都在我們的領域。在app.config如下

<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> 

如果我手動設置憑據的用戶名和密碼,一切正常,但是我一直在努力

managerService.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials 

,這樣我可以在傳遞當前用戶的憑據,但我在GetClients()調用中不會設置用戶名和密碼的錯誤。

任何人都可以幫助我嗎?我也嘗試加入

[OperationBehavior(Impersonation = ImpersonationOption.Required)] 

該方法,但這沒有什麼區別。

+0

我知道這聽起來很愚蠢,但你有沒有嘗試過**並沒有設置** ClientCredential'。我們使用'NetTcpBinding'來保證交通安全以及上述工作。 –

回答

6

你可以使用以下方法之一的用戶名(和更多的信息比其他的密碼):

//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); 

但是,有沒有辦法可以訪問該用戶的密碼。請參閱本question

一旦你收到的身份信息,你可以使用OutgoingMessageHeaders,如用戶名傳遞給WCF:

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

而在WCF服務實現,你可以讀取它喜歡:

OperationContext context = OperationContext.Current; 

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

請讓我知道這是否有幫助,或者如果您有任何其他問題。

謝謝, Soma。