2016-04-29 68 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。