How do I tell a WCF client proxy class to use windows authentication and the WindowsPrincipal of the already logged in domain user as credentials?

一个人想着一个人 提交于 2019-12-10 21:15:23

问题


I've got a WPF windows client that calls a WCF web service. The user is already logged in on the windows domain before starting the application and the WCF service uses windows authentication.

I want the WPF client to use the WindowsPrincipal of the already logged in user when calling the WCF service. I do NOT want to create a new NetworkCredential instance with an EXPLICIT username & password to do this, simply because asking the user to log in twice (in Windows and the app) is ... well pretty user unfriendly.

Most of the samples I've seen use this way to set the credentials, which is not what I want

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

Instead, I'd like to do something like this

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

That is, I want a NetworkCredential for the already existing (and working)

new WindowsPrincipal(WindowsIdentity.GetCurrent())

Does anybody know how to do this? I've tried setting security mode = "" and transport clientCredentialType = "" in app.config, but so far to no avail.


回答1:


Two things. Ensure that your WCF service is set to allow windows credentials. Once you've confirmed that you should be able to configure your client to use the Windows credential type. An example (from MSDN) is below.

WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;



回答2:


In your app.config:

  • Add:

    <system.net>
      <defaultProxy useDefaultCredentials="true"></defaultProxy>
    </system.net>
    
  • In your binding in element binding/security/transport, set proxyCredentialType="Ntlm"



来源:https://stackoverflow.com/questions/3166150/how-do-i-tell-a-wcf-client-proxy-class-to-use-windows-authentication-and-the-win

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!