在WCF服务中使用NetworkCredential [英] Using NetworkCredential in WCF services

查看:52
本文介绍了在WCF服务中使用NetworkCredential的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务,该服务使用Windows身份验证查看服务合同,并且服务中的特定方法配置为仅由特定用户UserX访问.

I have a WCF service which uses Windows Authentication to view Service Contract and a specfic method in a service is configured to be accessed only by a specific user UserX.

[PrincipalPermission(SecurityAction.Demand,Name="xxx\\UserA")]

在客户端,我需要访问上述服务方法.如果我使用的是Web参考->我添加以下内容

In the client side, I need to access the above service method. If I am using a Web Reference -> I add the following

client = new WebRefLocal.Service1();
client.Credentials = new System.Net.NetworkCredential("UserA", "xxxxxx", "test");

但是以上内容在WCF服务参考中无法实现,因为客户端凭据是只读的.实现以上目标的最佳方法之一是模拟 https://msdn.microsoft.com/en-us/library/ff649252.aspx.

But the above cannot be achieved in WCF Service Reference as Client Credentials are read-only. One best way I can achieve the above is Impersonation https://msdn.microsoft.com/en-us/library/ff649252.aspx.

我的问题是

  1. 为什么在WCF中将ClientCredentials设为只读?
  2. 网络凭据如何工作?他们将在客户端或服务器端对Windows登录名进行身份验证吗?
  3. 是否可以通过WCF在没有模仿的情况下实现上述目标?

推荐答案

我已经做了类似的事情-希望对您有所帮助:

I've done something like this - hope it helps:

 var credentials = new ClientCredentials();
credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation;
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("UserA", "xxxxxx", "test");

client.Endpoint.Behaviors.Remove<ClientCredentials>();
client.Endpoint.Behaviors.Add(credentials);

与具有以下安全设置的 BasicHttpBinding 一起使用:

Used with a BasicHttpBinding with following security settings:

  <security mode="TransportCredentialOnly">
    <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
  </security>

这篇关于在WCF服务中使用NetworkCredential的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆