我有一个 WCF 服务,它使用 Windows 身份验证来查看服务契约(Contract),并且服务中的特定方法被配置为只能由特定用户 UserX 访问。
[PrincipalPermission(SecurityAction.Demand,Name="xxx\\UserA")]
在客户端,我需要访问上面的服务方法。如果我使用网络引用 -> 我添加以下内容
client = new WebRefLocal.Service1();
client.Credentials = new System.Net.NetworkCredential("UserA", "xxxxxx", "test");
但上述内容在 WCF 服务引用中无法实现,因为客户端凭据是只读的。我实现上述目标的一种最佳方法是模拟 https://msdn.microsoft.com/en-us/library/ff649252.aspx 。
我的问题是
- 为什么 ClientCredentials 在 WCF 中设为只读?
- 网络凭证如何运作?他们会在客户端还是服务器端验证 Windows 登录?
- 有什么方法可以在 WCF 中实现上述目标而无需模拟吗?
最佳答案
我做过这样的事情 - 希望它有帮助:
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
一起使用:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="Windows" />
</security>
关于c# - 在 WCF 服务中使用 NetworkCredential,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346560/