- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在努力使 WCF 安全性适用于我的项目,但运气不佳。我正在尝试创建一个使用 net.tcp 作为绑定(bind)的服务,并实现消息和传输安全。消息安全是使用用户名和密码完成的,传输安全是(据说!)使用证书完成的。
为了我的开发测试,我创建了自己的证书颁发机构并将此证书放在我计算机的受信任存储区 (LocalMachine) 中。然后,我创建了两份证书,每份证书均由我的证书颁发机构签署,一份供服务使用,一份供客户端应用程序使用。我将这两个都放在 LocalMachine 的个人商店 (My) 中。然后,为了测试,我创建了一个随机证书,该证书不是由我的证书颁发机构签署的(因此不受信任),并将其放置在 LocalMachine 的个人商店中。我使用 makecert 创建了这些证书。
然后我将连接到该服务的客户端应用程序配置为使用无效的不受信任的证书作为其客户端证书。该服务被设置(据说)以使用链信任检查客户端证书。但是,此客户端能够连接并成功与服务对话!它应该被拒绝,因为它的证书不受信任!
我不知道是什么导致了这种行为,所以我将问题提交给你们,看看你们如何看待它。这是我的 WCF 配置:
服务配置:
<system.serviceModel>
<services>
<service behaviorConfiguration="DHTestBehaviour" name="DigitallyCreated.DHTest.Business.DHTestBusinessService">
<endpoint address="" binding="netTcpBinding" contract="DigitallyCreated.DHTest.Business.IDHTestBusinessService" bindingConfiguration="DHTestNetTcpBinding" bindingNamespace="http://www.digitallycreated.net/DHTest/v1" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8090/"/>
<add baseAddress="http://localhost:8091/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DHTestBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="DHTestMembershipProvider"/>
<serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectDistinguishedName" findValue="CN=business.dhtestDHTest.com" />
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" trustedStoreLocation="LocalMachine" revocationMode="NoCheck" />
</clientCertificate>
</serviceCredentials>
<serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="DHTestRoleProvider" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="DHTestNetTcpBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
<transport clientCredentialType="Certificate" protectionLevel="EncryptAndSign"/>
</security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
客户 session :
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IDHTestBusinessService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Certificate" protectionLevel="EncryptAndSign" />
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="DHTestBusinessServiceEndpointConf">
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectDistinguishedName" findValue="CN=invalid"/>
<serviceCertificate>
<authentication revocationMode="NoCheck" trustedStoreLocation="LocalMachine"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="net.tcp://phoenix-iv:8090/" binding="netTcpBinding"
behaviorConfiguration="DHTestBusinessServiceEndpointConf"
bindingConfiguration="NetTcpBinding_IDHTestBusinessService"
contract="DHTest.NetTcp.Business.IDHTestBusinessService"
name="NetTcpBinding_IDHTestBusinessService">
<identity>
<dns value="business.dhtest.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
客户端用户名/密码授权码:
DHTestBusinessServiceClient client = new DHTestBusinessServiceClient();
client.ClientCredentials.UserName.UserName = "ratfink";
client.ClientCredentials.UserName.Password = "testpassword";
提前感谢您的帮助。
编辑(2009/06/01):
我的一个 friend 向我指出了 a blog这回答了为什么会发生这种情况的问题。显然,当您指定 TransportWithMessageCredential 时,完全意味着:仅使用消息凭据进行传输。这就是为什么我的证书在传输级别被忽略的原因。
但是,我认为这个问题还没有完成和关闭,因为我仍然想这样做。 :) 我将研究我认为可以插入的自定义证书验证器,看看它是否有效。我会把结果回复给大家。
编辑(2009/06/08):
不,自定义证书验证器也不起作用。 WCF 根本不调用它们。
最佳答案
我找到了解决我的问题的方法,但是,它比我预期的要麻烦得多。
基本上,要实现传输和消息凭证检查,您需要定义自定义绑定(bind)。 (我找到了这方面的信息 here)。
我发现最简单的方法是继续在 XML 中进行配置,但在运行时复制并稍微修改 XML 配置中的 netTcp 绑定(bind)。实际上,您需要启用一个开关。这是服务端和客户端的代码:
服务端
ServiceHost businessHost = new ServiceHost(typeof(DHTestBusinessService));
ServiceEndpoint endpoint = businessHost.Description.Endpoints[0];
BindingElementCollection bindingElements = endpoint.Binding.CreateBindingElements();
SslStreamSecurityBindingElement sslElement = bindingElements.Find<SslStreamSecurityBindingElement>();
sslElement.RequireClientCertificate = true; //Turn on client certificate validation
CustomBinding newBinding = new CustomBinding(bindingElements);
NetTcpBinding oldBinding = (NetTcpBinding)endpoint.Binding;
newBinding.Namespace = oldBinding.Namespace;
endpoint.Binding = newBinding;
客户端
DHTestBusinessServiceClient client = new DHTestBusinessServiceClient();
ServiceEndpoint endpoint = client.Endpoint;
BindingElementCollection bindingElements = endpoint.Binding.CreateBindingElements();
SslStreamSecurityBindingElement sslElement = bindingElements.Find<SslStreamSecurityBindingElement>();
sslElement.RequireClientCertificate = true; //Turn on client certificate validation
CustomBinding newBinding = new CustomBinding(bindingElements);
NetTcpBinding oldBinding = (NetTcpBinding)endpoint.Binding;
newBinding.Namespace = oldBinding.Namespace;
endpoint.Binding = newBinding;
你可能以为就这样了,但你错了! :) 这是它变得特别蹩脚的地方。我将我的具体服务方法归因于 PrincipalPermission,以根据服务用户的角色限制访问,如下所示:
[PrincipalPermission(SecurityAction.Demand, Role = "StandardUser")]
一旦我应用了上述更改,这就开始失败了。原因是因为
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity
最终成为一个未知的、无用户名的、未经身份验证的身份。这是因为实际上有两种代表用户的身份:一种是用于通过传输进行身份验证的 X509 证书,另一种是用于在消息级别进行身份验证的用户名和密码凭据。当我对 WCF 二进制文件进行逆向工程以查看为什么它没有给我我的 PrimaryIdentity 时,我发现它有一行明确的代码,如果它找到多个 IIdentity,它会导致它返回空的 IIdentity。我猜这是因为它无法确定哪个是主要。
这意味着不能使用 PrincipalPermission 属性。相反,我编写了一个方法来模仿其可以处理多个 IIdentities 的功能:
private void AssertPermissions(IEnumerable<string> rolesDemanded)
{
IList<IIdentity> identities = OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["Identities"] as IList<IIdentity>;
if (identities == null)
throw new SecurityException("Unauthenticated access. No identities provided.");
foreach (IIdentity identity in identities)
{
if (identity.IsAuthenticated == false)
throw new SecurityException("Unauthenticated identity: " + identity.Name);
}
IIdentity usernameIdentity = identities.Where(id => id.GetType().Equals(typeof(GenericIdentity))).SingleOrDefault();
string[] userRoles = Roles.GetRolesForUser(usernameIdentity.Name);
foreach (string demandedRole in rolesDemanded)
{
if (userRoles.Contains(demandedRole) == false)
throw new SecurityException("Access denied: authorisation failure.");
}
}
它不是很漂亮(尤其是我检测用户名/密码凭证 IIdentity 的方式),但它确实有效!现在,在我的服务方法的顶部,我需要这样调用它:
AssertPermissions(new [] {"StandardUser"});
关于c# - 使用证书的 WCF 传输安全忽略链信任,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/929351/
WCF 服务、WCF RIA 服务和 WCF 数据服务之间有什么区别? 最佳答案 WCF 是一般服务的通信基础设施。 WCF RIA 服务自动生成客户端和服务器代理对象以方便应用程序开发,并依赖 WC
我想在我的 WPF 项目中使用 WCF 服务 (.svc)。, 我正在尝试创建一个服务。但在 Visual Studio 中,我们有“WCF 服务库”和“WCF 服务应用程序”。我两个都试了。 当我们
我正在开发 WCF Web 服务,并使用 WCF 服务应用程序模板来执行此操作。 创建“WCF 服务应用程序”是否满足此要求?与 WCF 服务应用程序相比,创建 WCF 服务库有哪些优势? 最佳答案
我是 WCF 的新手,对 Web 服务进行编码的经验有限。 在工作中,所有面向网络服务的事物都被要求使用 WCF。我需要做的工作涉及查询一个非 WCF Web 服务,该服务显然是用 Java 构建的,
我有一个数据契约(Contract)说用户。它是可序列化的并且可以通过网络传输。我想要一个操作契约(Contract) SaveUser()。我可以将 SaveUser(User user) 作为运营
我一直在开发一个使用 WCF 访问服务器端逻辑和数据库的 WPF 应用程序。 我从一个 WCF 客户端代理对象开始,我反复使用它来调用服务器上的方法。使用代理一段时间后,服务器最终会抛出异常: Sys
不要添加关于不同 WCF 堆栈的另一篇 SO 帖子,但我想在浪费更多开发时间之前确保我朝着正确的方向前进...... 我的场景 - 我们公司有许多 Web 应用程序,它们都访问同一系列的数据库。所有应
我是WCF技术的新手,我想知道RESTful WCF服务和普通WCF服务有什么区别。 RESTful 服务相对于普通 WCF 服务有哪些优势? 谢谢。 最佳答案 REST服务基于HTTP协议(prot
我正在构建的应用程序公开了多个 WCF 服务(A、B)。在内部,它消耗了在我们的内部网络(X、Y)上运行的其他几个 WCF 服务。 使用 WCF 消息日志记录,我希望仅记录我们的服务 A、B 与调用它
我们需要从另一个 WCF 服务调用 WCF 服务。为了测试这一点,我构建了一个示例控制台应用程序来显示一个简单的字符串。设置是: 控制台应用程序 -> WCF 服务 1 -> WCF 服务 2 Con
假设永远不会直接查询数据的情况。 AKA,总会有一些必须发生的过滤逻辑和/或业务逻辑。 什么时候是在 ajax/js 之外使用数据服务的好理由? 请不要访问此页面 http://msdn.micros
我在尝试将所有常规 WCF 调用转换为异步 WCF 调用时遇到问题。我发现我重构了很多代码,但不确定具体该怎么做。我使用了我找到的方法 here但遇到了我需要事情按顺序发生的问题。 private v
我在 IIS 上有一个 WCF 服务,一些 .net Web 应用程序正在使用它。我的任务是编写一个新的 WCF 服务,要求现有的 Web 应用程序可以使用新服务而无需更改它们的 web.config
我正在尝试用外部提供 WSDL 的 WCF 等效服务替换 WSE 服务。 首先,我使用 svcutil 和 wsdl 生成所有服务和客户端类(ATP,我只关心服务实现。)我生成了一个空的 WCF 服务
场景是这样的:有2个WCF Web Services,一个是客户端(WCFClient),一个是服务端(WCFServer),部署在不同的机器上。我需要他们两个之间的证书通信。 在服务器 WCF 上,
我在 Visual Studio 2013 中创建一个 WCF 服务并将其发布到 IIS。我可以在另一个项目中添加服务引用并使用该服务的方法。当我转到 IIS 服务器管理器时,我看到 WCF 激活及其
我是 .net 的新手,对 WCF 知之甚少,如果有任何愚蠢的问题,请耐心等待。我想知道如果我的代码没有显式生成任何线程,WCF 如何处理 SELF-HOST 场景中的同时调用。因此,在阅读了很多关于
我正在为应用程序开发一个面向服务的体系结构,我希望这些服务既可以通过 WCF 公开,也可以通过一个简单的库使用。理想情况下,我想减少重复代码。 从概念上讲,这映射到: Client => WCF Se
我有一个小型测试网络服务来模拟我在现实世界应用程序中注意到的一些奇怪的东西。由于演示显示与应用程序相同的行为,为了简洁起见,我将使用演示。 简而言之,我的服务接口(interface)文件如下所示(您
我首先为我的 WCF 服务启动了我的订阅者,然后继续发布我的发布者的帖子。我的订阅者能够收到帖子。 其次,我关闭了我的第一个订阅者并再次打开它以订阅相同的服务,即所谓的已订阅该服务的第二个订阅者。再一
我是一名优秀的程序员,十分优秀!