且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

消费与WPF windows应用程序集成的身份验证一个WebService

更新时间:2023-12-06 16:11:04

所以,事实证明,要解决这个问题,在我的情况很简单。

在对WebService的app.config文件中的绑定配置,我改变了这一点:

 <安全模式=无>
  <交通运输clientCredentialType =无proxyCredentialType =无的境界=/>
 <消息clientCredentialType =用户名algorithmSuite =默认/>
< /安全>
 

要这样:

 <安全模式=TransportCredentialOnly>
 <交通运输clientCredentialType =窗口proxyCredentialType =无的境界=/>
 <消息clientCredentialType =用户名algorithmSuite =默认/>
< /安全>
 

请注意,我改变了模式和clientCredentialType属性。

而在code后面我呼吁WebService的方法前加入这一行:

  client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
 

I have written a WPF 4.0 windows application that consumes a .net 3.5 WebService. This works fine when the web service in hosted to allow anonymous connections, however the WebService I need to consume when we go live will be held within a website that has Integrated Authentication enabled.

The person running the WPF application will be logged onto a computer within the same domain as the web server and will have permission to see the WebService (without entering any auth info) if browsing to it using a web browser that is NTLM auth enabled.

Is it possible to pass through the details of the already logged in user running the application to the WebService?

Here is the code I'm currently using:

MyWebService.SearchSoapClient client = new SearchSoapClient();
//From the research I've done I think I need to something with these:
//UserName.PreAuthenticate = true;
//System.Net.CredentialCache.DefaultCredentials;
List<Person> result = client.FuzzySearch("This is my search string").ToList();

Any pointers much appreciated.

Here is the error message I get when the call is currently made:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM,Digest qop="auth",algorithm=MD5-sess,nonce="+Upgraded+v17{hashremoved}",charset=utf-8,realm="Digest"'.

So it turns out that the solution to this problem is very simple in my case.

In the binding configuration for WebService in the App.Config file, I changed this:

<security mode="None">
  <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
 <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

To this:

<security mode="TransportCredentialOnly">
 <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
 <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

Note I changed the Mode and clientCredentialType attributes.

And in the Code Behind I added this line before calling the method on the WebService:

client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;