3

我有一个调用 WCF Web 服务的 WPF Windows 客户端。用户在启动应用程序之前已经登录到 windows 域,并且 WCF 服务使用 windows 身份验证。

我希望 WPF 客户端在调用 WCF 服务时使用已登录用户的 WindowsPrincipal。我不想使用 EXPLICIT 用户名和密码创建一个新的 NetworkCredential 实例来执行此操作,仅仅是因为要求用户登录两次(在 Windows 和应用程序中)是......非常用户不友好。

我见过的大多数示例都使用这种方式来设置凭据,这不是我想要的

serviceClientProxy.ClientCredentials.Windows.ClientCredential
= new NetworkCredential("username", "password", "domain");

相反,我想做这样的事情

serviceClientProxy.ClientCredentials.Windows.AllowedImpersonationLevel
    = TokenImpersonationLevel.Identification;
serviceClientProxy.ClientCredentials.Windows.ClientCredential
    = { /* network credential for already logged in user */ }

也就是说,我想要一个已经存在(并且正在工作)的 NetworkCredential

new WindowsPrincipal(WindowsIdentity.GetCurrent())

有人知道怎么做这个吗?我已经尝试在 app.config 中设置security mode = ""和传输clientCredentialType = "",但到目前为止无济于事。

4

2 回答 2

0

两件事情。确保您的 WCF 服务设置为允许 Windows 凭据。一旦您确认您应该能够将您的客户端配置为使用 Windows 凭据类型。下面是一个示例(来自 MSDN)。

WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
于 2010-07-07T13:51:23.640 回答
0

在您的 app.config 中:

  • 添加:

    <system.net>
      <defaultProxy useDefaultCredentials="true"></defaultProxy>
    </system.net>
    
  • 在元素绑定/安全/传输中的绑定中,设置 proxyCredentialType="Ntlm"

于 2010-07-07T14:00:32.107 回答