使用自托管WCF WebServiceHost进行模拟会收到“发生操作错误”。错误

时间:2011-12-21 16:55:49

标签: c# wcf security impersonation self-hosting

所以我一直在敲打这个。我有一个自托管的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服务的服务器(作为本地系统运行),并直接在该计算机上使用浏览器时,一切正常。这似乎是委托用户身份验证的问题......在这里仍然未知。

1 个答案:

答案 0 :(得分:0)

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

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