c# - 在模拟对数据库的 WCF 调用时遇到问题,显示 domain\Machine$ 登录失败

标签 c# asp.net wcf windows-services spring.net

我有一个托管在 Windows 服务中的 WCF 服务。 Windows 服务已安装并在本地系统帐户下运行。

我的目的是模拟调用者,检查调用者是否有足够的权限,如果有则让他继续,否则我会抛出错误异常。

DB 和 Windows 服务在同一台机器上。我们正在使用 Windows 身份验证。

Windows服务的app.config是:

<service name="ShoppingService" behaviorConfiguration="DefaultBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:9900/ShoppingService"/>
    </baseAddresses>
  </host>
  <endpoint address="" binding="wsHttpBinding" 
            contract="solution.IShoppingService">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="ShoppingServicemex" binding="basicHttpBinding" 
            contract="IMetadataExchange" />
</service>

行为是:

<behavior name="DefaultBehavior">
  <serviceMetadata httpGetEnabled="true"/>
  <serviceDebug includeExceptionDetailInFaults="true"/>
  <serviceAuthorization impersonateCallerForAllOperations="true"/>
</behavior>

WCF 实现是:

[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
public List<Job> GetItems()
{
    using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
    {
       string name = System.Threading.Thread.CurrentPrincipal.Identity.Name;
       return ItemDao.GetItems();
    }
}

客户端连接到服务并发送请求。当我调试 WCF windows 服务时,ServiceSecurityContext.Current.WindowsIdentity** 的值是该服务的调用方。这很好。

然后我模拟 ServiceSecurityContext.Current.WindowsIdentity.Impersonate()

问题是,当我调用 DAO ItemDao.GetItems(); 时,它通过以下连接访问数据库:Server=MyServer;Trusted_Connection=yes;Database=My_dev_db;

我收到错误信息:

System.ServiceModel.FaultException`1 was unhandled by user code
  Message=Could not get ADO.NET connection.
  Source=mscorlib
Login failed for user: domain\machinename$

它似乎是在使用 LocalSystem 用户帐户调用数据库。我想每次都模拟调用者,所以我检查他们是否有足够的安全权限来调用 DAO。

我正在使用 spring.net WCF 功能的 channel 工厂创建客户端。

我错过了什么?请指导?

最佳答案

我可能需要 Kerberos 才能执行此操作,这听起来像是经典的“Double Hop Problem”。

关于c# - 在模拟对数据库的 WCF 调用时遇到问题,显示 domain\Machine$ 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8184177/

相关文章:

c# - 将多个对象从 Angular Controller 发布到 Web API 2

c# - 有选择地为 RichTextBox 中的文本着色

c# - 使用 CONFIRM 获取用户输入

c# - 如何在自定义服务器控件 asp 中使用 .resx 和 .resource 文件?

WCF:如何在没有 WSDL:import 的情况下生成单个 WSDL 文档?

c# - 如何使用SharpDX绘制文字?

c# - 如何在Windows服务安装程序中设置 "interact with desktop"

c# - 如何从Azure WebJob读取站点的web.config文件?

c# - IIS7 进程回收后,两台服务器之间的 WCF 通信崩溃

c# - WCF 对比 socket