gpt4 book ai didi

c# - 在 WCF 服务中使用 NetworkCredential

转载 作者:行者123 更新时间:2023-11-30 21:55:24 27 4
gpt4 key购买 nike

我有一个 WCF 服务,它使用 Windows 身份验证来查看服务契约(Contract),并且服务中的特定方法配置为仅由特定用户 UserX 访问。

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

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

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

但由于客户端凭据是只读的,因此无法在 WCF 服务引用中实现上述内容。实现上述目标的一种最佳方法是模拟 https://msdn.microsoft.com/en-us/library/ff649252.aspx .

我的问题是

  1. 为什么 ClientCredentials 在 WCF 中是只读的?
  2. 网络凭据如何工作?他们会在客户端或服务器端对 Windows 登录进行身份验证吗?
  3. 有什么方法可以在不模拟的情况下在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com