WCF Windows服务数据库连接和模拟问题
我对 WCF 上的模拟有疑问。我想连接到由客户端应用程序调用的 WCF Windows 服务上的数据库。与数据库的连接应使用运行服务的帐户来完成。但我想验证对 WCF 服务的调用是否来自受信任的来源(验证客户端应用程序的用户是经过身份验证的域用户)。
您建议我使用哪种安全措施?
我尝试了模拟,但在尝试从 Windows 服务连接到数据库时出现此错误:
System.Data.SqlClient.SqlException:用户“NT AUTHORITY\ANONYMOUS LOGON”登录失败。
配置字符串是这样的:
server=myServer;初始目录=myDatabase;集成安全性=True
服务的 WCF 配置如下所示:
<system.serviceModel>
<services>
<service name="MyNamespace.MyService"
behaviorConfiguration="TransfertServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8095/MyNamespace.MyService"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="TransactionalBinding"
contract="myContract" />
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TransactionalBinding"
transferMode="Streamed" transactionFlow="true" maxReceivedMessageSize="1000000000">
<readerQuotas maxDepth="10000" maxStringContentLength="1000000000"
maxArrayLength="1000000000" maxBytesPerRead="10000" maxNameTableCharCount="10000" />
<security mode="Transport" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="TransfertServiceBehavior">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceAuthorization impersonateCallerForAllOperations="true" />
</behavior>
</serviceBehaviors>
</behaviors>
客户端应用程序上的配置如下所示:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_Client" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="true" transferMode="Streamed" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="1000000000"
maxBufferSize="1000000000" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1000000000"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8095/MyNamespace.MyService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Client"
contract="myContract" behaviorConfiguration="ImpersonationBehavior">
<identity>
<userPrincipalName value="[email protected]" />
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ImpersonationBehavior">
<clientCredentials>
<windows allowedImpersonationLevel="Impersonation" />
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
I have a question regarding the Impersonation on WCF. I'd like to connect to a DB on a WCF Windows service that is called by a client application. The connection to the DB should be done using the account under which the service runs. BUT I'd like to validate that the call to the WCF service is made from a trusted source (validate that the user of the client app is a autenticated user of the domain).
What is the kind security you would advise me to use ?
I tried Impersonation, but I get this error when trying to connect to the DB from the windows service :
System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
The configuration string is like this :
server=myServer;Initial Catalog=myDatabase;Integrated Security=True
The WCF configuration of the service looks like this :
<system.serviceModel>
<services>
<service name="MyNamespace.MyService"
behaviorConfiguration="TransfertServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8095/MyNamespace.MyService"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="TransactionalBinding"
contract="myContract" />
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TransactionalBinding"
transferMode="Streamed" transactionFlow="true" maxReceivedMessageSize="1000000000">
<readerQuotas maxDepth="10000" maxStringContentLength="1000000000"
maxArrayLength="1000000000" maxBytesPerRead="10000" maxNameTableCharCount="10000" />
<security mode="Transport" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="TransfertServiceBehavior">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceAuthorization impersonateCallerForAllOperations="true" />
</behavior>
</serviceBehaviors>
</behaviors>
The configuration on the client app looks like this :
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_Client" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="true" transferMode="Streamed" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="1000000000"
maxBufferSize="1000000000" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1000000000"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8095/MyNamespace.MyService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Client"
contract="myContract" behaviorConfiguration="ImpersonationBehavior">
<identity>
<userPrincipalName value="[email protected]" />
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ImpersonationBehavior">
<clientCredentials>
<windows allowedImpersonationLevel="Impersonation" />
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 WCF 模拟,则必须启用 Kerberos 约束委派 对于 WCF 帐户服务,另请参阅 WCF 的委托和模拟。
详细说明位于配置受约束委派信任的 WCF 服务标识:
If your WCF impersonates then you must enable Kerberos Constrained Delegation for the WCF account service, see also Delegation and Impersonation with WCF.
Detailed instructions at Configure the WCF Service Identity Trusted for Constrained Delegation:
从您的服务配置中删除此行:
并从您的客户端配置中删除此行:
模拟意味着所有操作都将在模拟用户的上下文中完成=服务的身份将替换为调用用户的身份。如果您的 SQL 服务器本地安装在运行 Windows 服务的计算机上,您对数据库的调用也将被模拟。
如果您关闭模拟,您将得到您想要的东西,因为服务中的执行将使用服务帐户,但服务将验证每个调用客户端。这是通过使用传输安全性和 Windows 集成身份验证的
netTcpBinding
配置来完成的。Remove this line from your service configuration:
and this from your client configuration:
Impersonation means that all operations will be done withing context of impersonated users = identity of the service is replaced with the identity of calling user. If your SQL server is installed locally on the machine where your Windows service is running your calls to database will be impersonated as well.
If you turn off impersonation you will have exactly what you want because execution in the service will use service account but service will authenticate each calling client. It is done by your
netTcpBinding
configuration which uses transport security with Windows integrated authentication.