使用 WCF 进行模拟
我试图通过 WCF Web 服务公开使用用户凭据访问 SQL 服务器(通过实体框架)的函数(这是客户端/dba 要求,因为审核触发器等。放弃尝试说服他们不要使用用户凭据)
我在让 WCF 实施模拟时遇到问题。 (事实上,这是一场噩梦)。我已将该
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
属性添加到我的合同中。但随后它抱怨 BasicHttpContract 无法进行窗口模拟。我尝试将其更改为 wsHttpBinding。现在元数据交换已损坏。到目前为止,我只是尝试在 Internet Explorer 中打开服务,甚至还没有开始与客户端连接。 web.config 的相关部分如下所示
<behaviors>
<serviceBehaviors>
<behavior name="AssetManagerBehavior">
<serviceAuthorization impersonateCallerForAllOperations="true" />
<!--
-->
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="AssetManager.ServiceInterface" behaviorConfiguration="AssetManagerBehavior">
<endpoint address="" binding="wsHttpBinding" contract="IAssetServices">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="test">
<security mode="TransportWithMessageCredential" />
</binding>
</wsHttpBinding>
</bindings>
相关服务接口类看起来如此
namespace AssetManager.ServiceInterface
{
[ServiceContract]
public interface IAssetServices
{
[OperationContract]
EmployeeDTO CreateNewEmployee(string firstName, string lastName);
}
}
实际实现如下所示
namespace AssetManager.ServiceInterface
{
public class AssetManagerServices : IAssetServices
{
#region IAssetServices Members
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
public EmployeeDTO CreateNewEmployee(string firstName, string lastName)
{
}
}
}
我正在使用 VS 2010、.net 4、IIS 7(启用了以下身份验证 - 匿名、ASP.NET 模拟、Basic身份验证、摘要式身份验证和 Windows 身份验证)。在 IIS 的根 Web 服务器上,我单击了 ISAPI 和 CGI 限制,并允许访问所有扩展(asp.net 4.0 可能是相关的)。
我当前的障碍是,它显示“此服务的元数据发布当前已禁用”。我在 IE 中浏览到该位置。这也不允许我添加服务引用。
最后一点,Windows 模拟肯定不会这么困难。使用 WCF 模拟 Web 服务的建议方法是什么?在 ASP.NET 世界中,只需添加
和
即可
I am trying to expose via WCF Web services, functions that access SQL server (via Entity Framework) using the user's credentials (this is a client/dba requirement because of audit triggers etc. Gave up trying to convince them not to use user's credentials)
I am having trouble getting WCF to implement impersonation. (in fact its been a nightmare). I have added the
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
attribute to my contract. But then it complained about BasicHttpContract not being able to do Window Impersonation. I've tried to change it to wsHttpBinding. Now the Metadata exchange is broken. So far, I am only trying to open the services in internet explorer, haven't even started connecting to it with the client yet. The relevant section of the web.config looks like below
<behaviors>
<serviceBehaviors>
<behavior name="AssetManagerBehavior">
<serviceAuthorization impersonateCallerForAllOperations="true" />
<!--
-->
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="AssetManager.ServiceInterface" behaviorConfiguration="AssetManagerBehavior">
<endpoint address="" binding="wsHttpBinding" contract="IAssetServices">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="test">
<security mode="TransportWithMessageCredential" />
</binding>
</wsHttpBinding>
</bindings>
The relevant service interface class looks so
namespace AssetManager.ServiceInterface
{
[ServiceContract]
public interface IAssetServices
{
[OperationContract]
EmployeeDTO CreateNewEmployee(string firstName, string lastName);
}
}
The actual implementation looks like this
namespace AssetManager.ServiceInterface
{
public class AssetManagerServices : IAssetServices
{
#region IAssetServices Members
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
public EmployeeDTO CreateNewEmployee(string firstName, string lastName)
{
}
}
}
I am using VS 2010, .net 4, IIS 7 (with following authentications enabled - Anonymous, ASP.NET impersonation, Basic Authentication, Digest Authentication and Windows Authentication). On the root web server in IIS, I clicked ISAPI and CGI Restrictions and allowed access to all extensions (asp.net 4.0 would probably be the relavent one)
My current hurdle is that it says "Metadata publishing for this service is currently disabled" when I browse to the location in IE. This also doesn't let me add service references.
As a final note, surely doing windows impersonation shouldn't be this difficult. What is the suggested way of doing impersonation for web services using WCF? In the asp.net world, it was as simple as adding <authentication mode="Windows"/>
and <identity impersonate="true"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用传输级安全性,那么它将使用 https 协议,而不是 http。
您需要设置
(您当前只有 httpGetEnabled)。
If you are using Transport level security, then it will be using the https protocol, not http.
You need to set
(You currently only have httpGetEnabled).