【问题标题】:Impersonation using a self-hosted WCF WebServiceHost gets 'An operations error occurred.' error使用自托管 WCF WebServiceHost 进行模拟获取“发生操作错误”。错误
【发布时间】:2012-01-25 11:29:07
【问题描述】:

所以我一直在努力解决这个问题。我有一个自托管的 WCF 服务:

var webServiceHost = new WebServiceHost(helloWorld);
webServiceHost.Authorization.ImpersonateCallerForAllOperations = true;

var uri = new Uri(BaseUri + webService.UriDirectory);
var webHttpBinding = new WebHttpBinding(webHttpSecurityMode);
webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

var sep = webServiceHost.AddServiceEndpoint(IHelloWorld, webHttpBinding, uri);
var webHttpBehavior = new WebHttpBehavior {HelpEnabled = true};
sep.Behaviors.Add(webHttpBehavior);

webServiceHost.Open();

我已经将以下属性应用到我的方法中:

[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public List<GpoItem> GetAll()
{
    using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
    {
        // Execute GPO code here...
        return new List<GpoItem>();
    }
}

为了添加更多上下文,我基本上有一个 Web 服务,它允许人们登录网页,在域上创建 GPO。在控制台中运行它可以正常工作,因为我以登录域用户的身份运行它。将其作为 Windows 服务运行,会引发“拒绝访问”异常。因此需要冒充。我在上面输入了以下更改后的代码,我得到'发生操作错误。 (来自 HRESULT 的异常:0x80072020)'。谷歌搜索显示它仍然是一个许可问题。我以管理员身份登录到 Web 服务测试环境,因此我拥有完全访问权限,并且我已经证明我可以以管理员身份在控制台中运行它。我觉得我在某些地方缺少一些标志设置。

有什么想法吗?

[Update1]我尝试将服务从作为本地系统运行切换到网络服务,但仍然遇到同样的问题。

[Update2] 当我登录到托管 WCF 服务的服务器(作为本地系统运行)并直接在该计算机上使用浏览器时,一切正常。委托用户身份验证似乎是一个问题......这里仍然未知。

【问题讨论】:

  • WindowsIdentity.Impersonate() 方法返回一个您在using 块中没有引用的WindowsImpersonationContext 值。引用 this 将在其超出范围时自动调用 WindowsImpersonationContext.Undo() 方法。
  • 您是否尝试过跟踪您的 WCF 服务以查看抛出异常的位置?
  • 我抛出的异常是'System.Runtime.InteropServices.COMException (0x80072020):发生操作错误。 (HRESULT 异常:0x80072020)'
  • 我确实曾经引用过 WindowsIdentity.Impersonate(),但从网上的示例中看到,我不需要引用它。无论如何,当我确实提到它时,它没有任何区别。
  • 您的服务是否在域控制器上运行,或者是否需要任何其他网络跃点来创建 GPO?它看起来像代表团的问题。模拟仅适用于单跳 = 从客户端到托管服务的服务器,但不会模拟所有后续网络调用。只有使用委派和 Kerberos 才能做到这一点。

标签: c# wcf security impersonation self-hosting


【解决方案1】:

显然,只能从 WCF 属性中获取模拟级别的令牌,该令牌只能用于本地资源访问。相反,我必须使用 LogonUser API,以便我可以获得具有网络资源访问权限的委托级令牌。 [1]

[1]http://msdn.microsoft.com/en-us/library/ff649252.aspx

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2012-09-05
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多